ASP Core 2空POST请求

时间:2018-02-17 15:37:11

标签: c# json asp.net-core http-post

我有一个简单的HTTP POST请求,我发送到使用Kestrel在localhost中运行的ASP Core 2应用程序。收到的数据始终为null,除非我使用其他C#应用中的PostAsJsonAsync

我发送的json有以下表格:

{
  "_isValid": true,
  "_username": "test",
  "_guid": "f3f574eb-5710-43c5-a4ff-0b75866a72a7",
  "_dt": "2018-02-11T15:53:44.6161198Z",
  "_ms": "IsSelected"
  [...]
}

ASP控制器具有以下形式:

// POST: api/NativeClient
[HttpPost]
public async Task<IActionResult> Post([FromBody]string value)
{
   [...]

1。案例:使用PostAsJsonAsync从另一个C#应用

发送

在一个案例中,我通过另一个使用PostAsJsonAsync的单独的C#应用​​程序成功发送请求:

HttpClient client = new HttpClient();
client.BaseAddress = new System.Uri("http://localhost:51022/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = await client.PostAsJsonAsync("/api/NativeClient/", json);

控制器接收呼叫并成功填充value

2。案例:使用Postman等外部REST客户端发送

在另一种情况下,我尝试通过Postman通过另一个REST客户端发送请求,例如REST client extension或Visual Studio Code:

POST http://localhost:51022/api/NativeClient/ HTTP/1.1
content-type: application/json; charset=utf-8

{
  "_isValid": true,
  "_username": "gnappo",
  "_guid": "f3f574eb-5710-43c5-a4ff-0b75866a72a7",
  "_dt": "2018-02-11T15:53:44.6161198Z",
  "_ms": "IsSelected"
  [...]
}

此处请求由控制器接收,但string value始终为null

我尝试删除[FromBody]标记,检查请求标头,检查正文中的json字符串(完全相同),以及下面引用中提到的其他内容,但没有任何作用。< / p>

我错过了什么?

其他测试/参考

Value are always null when doing HTTP Post requests

Asp.net Core 2 API POST Objects are NULL?

Model binding JSON POSTs in ASP.NET Core

2 个答案:

答案 0 :(得分:4)

您应该将JSON反序列化为某个模型(类),因为我相信您在代码中的任何位置反序列化此字符串:

public class YourModel
{
    public bool _isValid { get; set; }
    public string _username { get; set; }
    public string _guid { get; set; }
    public DateTime _dt { get; set; }
    public string _ms { get; set; }
    // other properties from json
}

你的行动:

[HttpPost]
public async Task<IActionResult> Post([FromBody] YourModel value)

关于您的应用:

在您的应用中,您将json作为参数传递给PostAsJsonAsync(),但您需要传递对象并将其解析为JSON

var response = await client.PostAsJsonAsync("/api/NativeClient/", yourObject);

yourObject是类的实例,应该解析为JSON

评论后:

另外,请尝试将您的应用代码更改为:

HttpClient client = new HttpClient();
client.BaseAddress = new System.Uri("http://localhost:51022/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var httpContent = new StringContent(json, Encoding.UTF8, "application/json");    
var response = await client.PostAsync("/api/NativeClient/", httpContent);

答案 1 :(得分:1)

如果您想在API HttpClient中使用此标题收到client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); 正文,请在// POST: api/NativeClient [HttpPost] public async Task<IActionResult> Post([FromBody]string value) { [...] 请求中使用此标题:

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

将接收正文作为字符串。

API

如果您需要接收内容作为对象,请将此标头添加到您的请求中:

// POST: api/NativeClient
[HttpPost]
public async Task<IActionResult> Post([FromBody]yourClass class)
{
   [...]

并像这样更改JSON

API

var Content = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync("/api/NativeClient/", Content); 个对象发送到您的PostAsJsonAsync

JSON

PostAsync需要一个C#对象将其序列化为JSON字符串并发送,但GROUP_CONCAT需要SELECT orders.created_at,products.title, o_p.qty AS qty, (SELECT GROUP_CONCAT(features.title,"\:", o_p_f.value, features.unit) FROM order_product_features AS o_p_f LEFT JOIN product_features ON product_features.id = o_p_f.product_feature_id LEFT JOIN features ON o_p_f.product_feature_id = product_features.id AND features.id = product_features.feature_id WHERE o_p_f.order_product_id = o_p.id ) AS prop FROM orders LEFT JOIN order_products AS o_p ON o_p.order_id = orders.id LEFT JOIN products ON products.id = o_p.product_id 字符串才能发送。

请参阅此Q&amp; A httpclient-not-supporting-postasjsonasync-method-c-sharp

请参阅此链接以获取更多信息:

PostAsJsonAsync in C#

PostAsync