1

I am trying to call one of my APIs POST method from my client code using httpclient. The problem is that I want to pass array of integer(ids) as request body as per my destination APIs POST method, but I am not able to call this method using my source code.

Could anyone help me ?

Destination code:

public IActionResult GetByIds([FromBody] int[] ids)
{
 try
 {
 var collterals = _collateralRepository.GetCollateralsByIds(ids);
 return Ok(collterals);
 }
 catch (Exception)
 {
 throw;
 }
}

Source Code:

int[] ids = outputsummary.Select(x => x.Collateral_Id).ToArray();
var data = new StringContent(JsonConvert.SerializeObject(ids), Encoding.UTF8, "application/json");
var collateralClient = _httpClientFactory.CreateClient("CollateralClient");
var response = await collateralClient.PostAsync("Collateral/GetByIds", data);
Rahul Sharma
8,3772 gold badges38 silver badges58 bronze badges
asked Mar 21, 2022 at 7:56
2
  • 1
    What's the issue you are facing? Any error? Can you share the error details? Commented Mar 21, 2022 at 8:10
  • 3
    catch (Exception) { throw; } well, that doesn't add any value... Commented Mar 21, 2022 at 9:08

2 Answers 2

1

It seems that WEB API is not able to get the data correctly since it does not deal with multiple posted content values. The best way for your case would be to create a Model class that will hold your int[] and then you can POST it to your API.

public class MyIDModel
{
 public int[] ids{ get; set; }
}

So your source code will be:

int[] ids = outputsummary.Select(x => x.Collateral_Id).ToArray();
MyIDModel mymodel=new MyIDModel();
mymodel.ids=ids;
var data = new StringContent(JsonConvert.SerializeObject(mymodel), Encoding.UTF8, "application/json");
var collateralClient = _httpClientFactory.CreateClient("CollateralClient");
var response = await collateralClient.PostAsync("Collateral/GetByIds", data);

Your destination code will remain the same.

answered Mar 21, 2022 at 9:15

1 Comment

Thanks for your quick response Yes this i already tried and it works fine with the model, was just thinking without creating a model can we do it or not? and Why?
1

I don't know why you can not send data without wraping using json, I tested your code using common httpclient and it is working properly at my mashine. The problem coud be in your named client _httpClientFactory.CreateClient("CollateralClient");

But there is another way if you want to sent an array without wraping it in a viewmodel, you can try this code that is using application/x-www-form-urlencoded content

 int[i] ids=..your code
 int counter = 0;
 var values = new List<KeyValuePair<string, string>>();
 foreach (int i in ids)
 {
 values.Add(new KeyValuePair<string, string>("ids[" + counter.ToString() + "]", i.ToString()));
 counter++;
 }
 
 var content = new FormUrlEncodedContent(values);
 
 var response = await collateralClient.PostAsync("Collateral/GetByIds", content);

and remove [frombody]

public IActionResult GetByIds(int[] ids) 
answered Mar 21, 2022 at 13:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.