Asp.net核心2.0 POST端点在没有json有效负载的情况下工作,但是失败(400 Bad请求)和json有效负载

时间:2018-04-09 15:17:16

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

我的asp.net core 2.0 app中有以下端点:

[HttpPost("postself")]
public IActionResult post([FromBody]JObject email)
{
    try
    {
        var service = new EmailService();
        var emailHtml = service.GenerateEmail(email.ToString(), false);
        return Json(new { Email = emailHtml });
    }
    catch
    {
        return Json(new { Email = "error" });
    }
}

如此调用API:

curl -X POST \
  http://myapp/api/v1/emails/postself \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 85c9bbe7-2112-0746-5f41-8dc01b52ab59'

端点被点击并在return Jason(new { Email = "error" });返回,因此可行。它返回error,因为JObjectnull,但这仍然是预期的行为。

但是,当像这样调用API时(使用JSON有效负载):

curl -X POST \
  http://myapp/api/v1/emails/postself \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: a80c85de-8939-01d8-4a5d-c2108bf1491d' \
  -d '{
  "body": [
    {
      "image": {
        "url": "mailing_logo_slice.png",
        "alt": "This is my logo"
      }
    }
  ]
}'

...我的应用返回400 Bad request

此外,请求在开发中工作,但只在生产中返回400

有什么想法吗?

======

更新的代码:

[HttpPost("postself")]
public IActionResult PostSameDomain([FromBody]string email)
{
    try
    {
        var service = new EmailGenerationService();
        var emailHtml = service.GenerateEmailFromJson(email, false);
        return Json(new { Email = emailHtml });
    }
    catch
    {
        return Json(new { Email = "error" });
    }
}

[HttpPost("test")]
        public IActionResult PostSameDomain([FromBody]EmailViewModel email)
        {
            try
            {
                var service = new EmailGenerationService();
                var emailHtml = service.GenerateEmailFromJson(email.Raw, false);
                return Json(new { Email = emailHtml });
            }
            catch
            {
                return Json(new { Email = "error" });
            }
        }

3 个答案:

答案 0 :(得分:2)

JObject特定于Newtonsoft.Json,不起作用。请指定包含urlalt属性的POCO类。

答案 1 :(得分:2)

我相信你的问题是你传入JSON数组并尝试将其反序列化为单数JObject

可能的修复:

  1. 传入一个JSON对象:

    curl -X POST \
      http://myapp/api/v1/emails/postself \
      -H 'cache-control: no-cache' \
      -H 'content-type: application/json' \
      -H 'postman-token: a80c85de-8939-01d8-4a5d-c2108bf1491d' \
      -d '{
      "body": {
          "image": {
            "url": "mailing_logo_slice.png",
            "alt": "This is my logo"
          }
        }
    }'
    
  2. 切换为JArray而不是JObject

    [HttpPost("postself")]
    public IActionResult post([FromBody]JArray email)
    {
        try
        {
            var service = new EmailService();
            var emailHtml = service.GenerateEmail(email.ToString(), false);
            return Json(new { Email = emailHtml });
        }
        catch
        {
            return Json(new { Email = "error" });
        }
    }
    
  3. 将数据作为string接收并在其中反序列化。这将允许您测试您是否已收到数组或单个对象,然后根据需要反序列化为JObjectJArray(这将是我个人推荐的价值)。

答案 2 :(得分:0)

对于遇到此问题的其他人,this是问题的根源。

感谢大家一起思考我的问题。