Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post anonymous object via HttpClient

I'm trying to post anonymous object via httpclient, however orderId is null and collection empty when it hits controller.

    public async Task<Response> CancelOrderAsync(int orderId, ICollection<int> ids)
    {
        Response result = null;
        using (IHttpClient client = HttpClientFactory.CreateHttpClient())
        {
            var obj = new {OrderId = orderId, Ids = ids};
            string json = JsonConvert.SerializeObject(obj);
            HttpContent postContent = new StringContent(json, Encoding.UTF8, "application/json");

            using (var response = await client.PostAsync($"{url}/admin/cancel", postContent).ConfigureAwait(false))
            {
                if (response != null && response.IsSuccessStatusCode)
                {
                    ...
                }
            }
        }

        return result;
    }


    // Controller
    [HttpPost]
    [ActionName("cancel")]
    public async Task<Response> Cancel(int orderId, ICollection<int> ids)
    {
        // order is null, collection empty
        ...

EDIT:

Changed my controller to this for simplicity

    [HttpPost]
    [ActionName("cancel")]
    public async Task<SimpleResponse> Cancel(int orderId)

Via Postman, i'm posting this body:

{
  "orderId": "12345"
}

Still, orderId comes in as 0 (zero) ??

like image 371
ShaneKm Avatar asked Nov 18 '25 20:11

ShaneKm


2 Answers

The controller action on the server side will need a concrete type to read the entire body of the request

public class Order {
    public int OrderId { get; set; }
    public int[] Ids { get; set; }
}

This is primarily because the action can only read from the body once.

Update action to...

[HttpPost]
[ActionName("cancel")]
public async Task<Response> Cancel([FromBody]Order order) {
    if(ModelState.IsValid) {
        int orderId = order.OrderId;
        int[] ids = order.Ids;
        //...
    }
    //...
}

the original code used to send the request in the example will work as is, but as mentioned it can be improved.

like image 155
Nkosi Avatar answered Nov 21 '25 10:11

Nkosi


The HttpClient can do the serialisation for you. See if

var response = await client.PostAsJsonAsync($"{url}/admin/cancel", obj);

works better. Then you don't need to write the serialisation code yourself.

If you still have a problem, use a tool such as Fiddler to monitor the actual request and see what parameter and values are submitted in the request body, to see if they match what's expected by the endpoint.

like image 41
ADyson Avatar answered Nov 21 '25 09:11

ADyson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!