Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpRequestMessage.Content is null in receiving Controller action

I've looked at some similar posts, but all had some relevant detail that does not apply in my case. I have an existing Shopper service with a Register method. It is built on .NET Framework 4.6.1 Web API. I have a number of working scenarios in which another .NET Framework 4.6.1 Web API service calls the Shopper service using HttpClient and HttpRequestMessage. I do this with GET, PUT, and POST methods and successfully pass data to the PUT and POST methods using

request.Content = new ObjectContent<MemberAddress>(memberAddress, new System.Net.Http.Formatting.JsonMediaTypeFormatter());

I'm now developing a new service, this one built on ASP.NET Core Web API. I'm attempting to call a POST action in the Shopper service. I'm getting my HttpClient from IHttpClientFactory.CreateClient. The HttpRequestMessage set up is, I think, the same as in my other calling services.

        var request = new HttpRequestMessage(HttpMethod.Post, updateShopperUrl);
        request.Content = new ObjectContent<MemberRegistration>(memberRegistration, new System.Net.Http.Formatting.JsonMediaTypeFormatter(), "application/json");

The call to the service looks like this:

        var httpClient = _clientFactory.CreateClient();
        var response = await httpClient.SendAsync(request);

I can inspect request.Content.Value before the call and it contains the object/data I expect. The controller action code on the other end looks like this:

    [Route("{shopperId}/register")]
    [Route("~/api/shopper/{shopperId}/register")]
    [HttpPost]
    public IHttpActionResult RegisterNewMember(string shopperId, [FromBody] MemberRegistration memberRegistration)
    {

But the memberRegistration parameter is always null. The [FromBody] attribute is recent addition in an attempt to solve this problem, but it did not help. FromBody should be the default behavior for a complex object parameter anyway. I can POST to that endpoint with Postman and the memberRegistration data comes through.

Either I'm just missing something obvious or maybe there's something different happening in the ASP.NET Core calling side of the equation.

like image 306
Zoe Avatar asked Jun 13 '26 12:06

Zoe


1 Answers

It appears you are trying to post JSON data

Try changing the approach a bit and see if it make a difference.

var json = JsonConvert.SerializeObject(memberRegistration);

var content = new StringContent(json, Encoding.UTF8,"application/json");

var httpClient = _clientFactory.CreateClient();

var response = await httpClient.PostAsync(updateShopperUrl, content);

The above manually serializes the object to JSON and Posts it to the web API.

It is possible there could have been an issue with the formatter used with the ObjectContent

like image 64
Nkosi Avatar answered Jun 16 '26 15:06

Nkosi



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!