HttpClient PostAsync posting null content

10,675

As indicated in the comments the model was not being converted to JSON when you called model.ToString. You eventually figured out that you can use Json.Net to serialize the model to JSON with JsonConvert.SerializeObject(model). This will work for serializing the model to JSON.

You could go one step further and create an extension method to perform that functionality for you

public class JSONStringExtension {
    public static string ToJsonString(this object model) {
        if(model is string) throw new ArgumentException("mode should not be a string");
        return JsonConvert.SerializeObject(model);
    }
}

This will now allow you to call the method on your model and covert it to JSON in your code.

var baseUri = new Uri("http://localhost:5001/"):
_httpClient.BaseAdress = baseUri;
var data = new StringContent(content: model.ToJsonString(), //<--Extension method here
             encoding: Encoding.UTF8, 
             mediaType: "application/json");

var response = await _httpClient.PostAsync("api/product", data);

The PostAsJsonAsync extension method that is frequently used basically performs the same thing you eventually realized by abstracting the JSON serialization step for you. internally it is calling the same PostAsync method.

which would look something a little like this

public static Task<HttpResponseMessage> PostAsJsonAsync(this HttpClient httpClient, string url, object content) {
    var json = JsonConvert.SerializeObject(content)
    var data = new StringContent(content: json,
                 encoding: Encoding.UTF8, 
                 mediaType: "application/json");
     return httpClient.PostAsync(url, data);
}
Share:
10,675
rethabile
Author by

rethabile

I get more joy out of solving programming problems. I am a continuous learner who loves to experiment with technologies, frameworks, and things. I can pick up new stuff very quickly. by night: I read everything software technology for fun: C# by day: full-stack in .NET and JavaScript.

Updated on July 01, 2022

Comments

  • rethabile
    rethabile almost 2 years

    I have one api calling another.

    and here is my code which seems to cause ModelState.IsValid = false on the other side of world.

    var baseUri = new Uri("http://localhost:5001/"):
    _httpClient.BaseAdress = baseUri;
    var data = new StringContent(content: model.Tostring(),
                 encoding: Encoding.UTF8, 
                 mediaType: "application/json");
    
    var response = await _httpClient.PostAsync("api/product", data);
    

    watching Post([FromBody]Product product) on the api being called I just see product=null.

    changing to Post([FromBody]object product) also shows null.

    calling the api from Postman works perfectly fine. which localize my problem to PostAsync. what's going on with my PostAsync?

    Edit:

    I know people might suggest PostAsJsonAsync, but I'll try it only after I know what's the problem with PostAsync. :(