简体   繁体   English

HttpClient PostAsync发布空内容

[英]HttpClient PostAsync posting null content

I have one api calling another. 我有一个API调用另一个。

and here is my code which seems to cause ModelState.IsValid = false on the other side of world. 这是我的代码,似乎在世界的另一端导致ModelState.IsValid = false

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 . 在被称为api上观看Post([FromBody]Product product)我只看到product=null

changing to Post([FromBody]object product) also shows null . 更改为Post([FromBody]object product)也显示null

calling the api from Postman works perfectly fine. Postman调用api可以正常工作。 which localize my problem to PostAsync . 这将我的问题本地化为PostAsync what's going on with my PostAsync ? 我的PostAsync怎么了?

Edit : 编辑

I know people might suggest PostAsJsonAsync , but I'll try it only after I know what's the problem with PostAsync . 我知道人们可能会建议PostAsJsonAsync ,但是只有在知道PostAsync什么问题之后,我才会尝试。 :( :(

As indicated in the comments the model was not being converted to JSON when you called model.ToString . 如注释中所述,当您调用model.ToString时,该model未转换为JSON。 You eventually figured out that you can use Json.Net to serialize the model to JSON with JsonConvert.SerializeObject(model) . 最终您发现可以使用Json.Net通过JsonConvert.SerializeObject(model)将模型序列化为JSON。 This will work for serializing the model to JSON. 这将用于将模型序列化为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. 现在,这将允许您在模型上调用方法并将其隐式转换为代码中的JSON。

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. 经常使用的PostAsJsonAsync扩展方法通过为您抽象JSON序列化步骤,基本上完成了您最终实现的功能。 internally it is calling the same PostAsync method. 在内部,它正在调用相同的PostAsync方法。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM