【问题标题】:Can't read request from postman in webapi无法在 webapi 中读取邮递员的请求
【发布时间】:2021-04-06 11:55:36
【问题描述】:

我的 webapi 有问题。 我试图向我的 webapi 发送一个发布请求,但我总是在里面接收 null 值。 我在邮递员请求中的身体看起来如何:

{
  "languageCharset" : 1045,
  "jsonBody" : [... WHOLE JSON HERE ...]
}

请求命中正确,因为我可以调试它,但总是将 languageCharset = null 和 jsonString 作为空字符串。这就是它在 c# 代码中的样子:

[...]
[HttpPost("")]
    [Route("")]
    public IActionResult Post([FromBody] RequestJSONFromBody requestJSONFromBody)
    {
      requestJSONFromBody = requestJSONFromBody;
      return new OkObjectResult(requestJSONFromBody);
    }
[...]

和 RequestJSONFromBody :

using DatabaseConnection.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DatabaseConnection.Models
{
  public class RequestJSONFromBody
  {
    LanguageCharset languageCharset { get; set; }
    string jsonBody { get; set; }

  }
}

#更新 1

语言字符集枚举:

namespace DatabaseConnection.Types
{
  public enum LanguageCharset : short
  {
    pl = 1045,
    en = 1033,
    cs = 1029,
    ro = 1048,
    lv = 1062,
    de = 1031,
    lt = 1063,
    sk = 1051,
    tr = 1055,
    et = 1061,
    hu = 1038,
    ru = 1049,
    sl = 1060,
    rs = 2074,
    fi = 1035,
    no = 1044,
    at = 3079,
    fr = 1036,
    bg = 1026,
    da = 1030,
    gr = 1032,
    hr = 1050,
    it = 1040,
    nl = 1043,
    pt = 2070,
    sv = 1053,
    ua = 1058,
    be = 1059,
    es = 1034,
    mk = 1071,
    ka = 1087
  }
}

邮递员 POST 路径:

https://localhost:44397/api/translation/

【问题讨论】:

  • 什么是LanguageCharset - 枚举?
  • @Genusatplay 我更新了更多细节
  • Postman 的其他详细信息是什么?内容类型等?如果您单击“代码”链接/按钮,选择 HTTP 作为语言,并使用它向您显示的内容更新您的问题,这可能会有所帮助。
  • 其余都是默认的,没有任何改变
  • 顺便说一句,您在请求正文中显示了“jsonString”,但类属性显示为“jsonBody”。

标签: c# .net-core postman webapi


【解决方案1】:

你放弃了public。 然后您发送jsonString 并接收jsonBody

public class RequestJSONFromBody
{
   public LanguageCharset languageCharset { get; set; }
   public string jsonString { get; set; }
}

邮递员:
json 发送 Body->rawJSON 类型

【讨论】:

  • 我在重写代码发布时的错误。在我的代码中,我到处都有 jsonCode,添加 pubic 无济于事
  • 你需要用实际数据更新问题。
【解决方案2】:

问题出在 json 内部(缺少 ")。问题已解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-19
    • 2019-05-03
    • 2020-12-11
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多