400错误的请求,通过HttpClient.PutAsync将Json提交到WebApi

时间:2019-02-06 17:22:04

标签: c# json asp.net-core-webapi dotnet-httpclient

通常,从服务到Webapi调用都将使用序列化对象,但在这种情况下,我必须使用json表示该调用。

过程将是将json反序列化为适当的类,然后照常进行处理。

HttpClient放置

从控制台应用程序中调用方法

   public async Task<ApiMessage<string>> PutAsync(Uri baseEndpoint, string relativePath, Dictionary<string, string> headerInfo, string json)
    {
        HttpClient httpClient = new HttpClient();
        if (headerInfo != null)
        {
            foreach (KeyValuePair<string, string> _header in headerInfo)
                _httpClient.DefaultRequestHeaders.Add(_header.Key, _header.Value);
        }

        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json-patch+json"));

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

        var response = await httpClient.PutAsync(CreateRequestUri(relativePath, baseEndpoint), content);
        var data = await response.Content.ReadAsStringAsync();

        ... 
    }

端点

该呼叫永远不会到达端点。如果删除[FromBody]标记,则端点被命中,但是按预期,该参数为null。似乎正在发生某种过滤。

    [HttpPut()]
    [Route("")]
    [SwaggerResponse(StatusCodes.Status200OK)]
    [SwaggerResponse(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> UpdatePaymentSync([FromBody] string paymentSyncJson)
    {
        if (string.IsNullOrEmpty(paymentSyncJson))
            return BadRequest();
         //hack: don't have access to models so need to send json rep
         var paymentSync = JsonConvert.DeserializeObject<PaymentSync>(paymentSyncJson);
       ....
    }

这是json负载。我以为[FromBody]处理了简单类型,但这证明我错了。

  {
    "paymentSyncJson": {
      "id": 10002,
      "fileName": "Empty_20190101.csv",
      "comments": "Empty File",
      "processingDate": "2019-01-02T19:43:11.373",
      "status": "E",
      "createdDate": "2019-01-02T19:43:11.373",
      "createdBy": "DAME",
      "modifiedDate": null,
      "modifiedBy": null,
      "paymentSyncDetails": []
    }
  }

2 个答案:

答案 0 :(得分:1)

您的有效负载不是字符串,而是json,这就是运行时无法将正文解析为您请求的string paymentSyncJson的原因。

要解决此问题,请创建一个反映json的匹配dto

public class PaymentDto
{
    public PaymentSyncDto PaymentSyncJson { get; set; }
}
public class PaymentSyncDto
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string Comments { get; set; }
    public DateTime ProcessingDate { get; set; }
    public string Status { get; set; }
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
    public int[] PaymentSyncDetails { get; set; }
}

然后在控制器方法中使用它从请求正文中读取数据

public async Task<IActionResult> UpdatePaymentSync([FromBody] PaymentDto payment)

答案 1 :(得分:1)

只需扩展我的评论即可。

OP做了:

[HttpPut()]
[Route("")]
[SwaggerResponse(StatusCodes.Status200OK)]
[SwaggerResponse(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdatePaymentSync([FromBody] string paymentSyncJson)
{
    if (string.IsNullOrEmpty(paymentSyncJson))
        return BadRequest();
     //hack: don't have access to models so need to send json rep
     var paymentSync = JsonConvert.DeserializeObject<PaymentSync>(paymentSyncJson);
   ....
}

在他们放置[FromBody] string paymentSyncJson的地方,FromBody将尝试反序列化为您指定的类型,在这种情况下为string。我建议这样做:

public async Task<IActionResult> UpdatePaymentSync([FromBody] JObject paymentSyncJson)

然后您可以更改此行:

var paymentSync = JsonConvert.DeserializeObject<PaymentSync>(paymentSyncJson);

收件人:

var paymentSync = paymentSyncJson.ToObject<PaymentSync>();