【问题标题】:(400) Bad Request when trying to post simple value to an API(400) 尝试向 API 发布简单值时出现错误请求
【发布时间】:2018-12-10 11:16:42
【问题描述】:

我有这个 LoansController 用于 web api

[Route("api/[controller]")]
[ApiController]
public class LoansController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // POST api/loans
    [HttpPost]
    public void Post([FromBody] string value)
    {

    }
}

在 PowerShell 中我可以调用

Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Get

它工作正常(我得到value1value2

但是当我尝试时

$postParams = "{'value':'123'}"
Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Post # -ContentType 'Application/json'

我只是不断得到

Invoke-WebRequest : 远程服务器返回错误:(400) Bad Request。

我做错了什么?

我尝试添加-ContentType 'Application/json',但没有任何区别

我错过了什么?

我也尝试了Invoke-RestMethod,但结果相同..

接下来我从value 参数中删除了[FromBody],但value 现在以null 的形式出现

【问题讨论】:

  • 发送实际的 JSON 字符串 $postParams = "\"123\"" 或创建一个与 json 匹配的模型并在操作中使用它。最后确保内容类型设置为正确的类型
  • 获取请求没有正文

标签: powershell asp.net-web-api asp.net-core


【解决方案1】:

原因

这只是发生了因为您的操作方法期望来自 HTTP 请求正文的普通 string

[HttpPost]
public void Post([FromBody] string value)
{

}

这里的纯字符串是由""引用的字符序列。换句话说,要表示字符串,您需要在向此操作方法发送请求时在这些字符前后包含引号

如果您确实想将 json 字符串 {'value':'123'} 发送到服务器,则应使用以下有效负载:

POST http://localhost:1113/api/loans HTTP/1.1
Content-Type: application/json

"{'value':'123'}"

注意:我们必须使用双引号字符串!不要发送没有""的字符串

如何解决

  1. 要发送纯字符串,只需使用以下PowerShell 脚​​本:

    $postParams = "{'value':'123'}"
    $postParams = '"'+$postParams +'"'
    Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams  -Method Post  -ContentType 'application/json'
    
  2. 或者,如果您想使用 json 发送有效负载,您可以创建一个 DTO 来保存 value 属性:

    public class Dto{
        public string Value {get;set;}
    }
    

    并将您的操作方法更改为:

    [HttpPost]
    public void Post(Dto dto)
    {
        var value=dto.Value;
    }
    

    最后,您可以调用以下PowerShell 脚本来发送请求:

    $postParams = '{"value":"123"}'
    Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams  -Method Post  -ContentType 'application/json'
    

这两种方法对我来说都完美无缺。

【讨论】:

    【解决方案2】:

    尝试添加标题

    $url = 'http://localhost:1113/api/loans'
    $head = @{'ContentType'='Application/json'}
    Invoke-WebRequest -Uri $url -Body $postParams -Method Post -Headers $head 
    

    【讨论】:

      【解决方案3】:

      首先,尝试在powershell命令中使用ConvertTo-Json

      $postParams = @{ "value": "123"} | ConvertTo-Json
      Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Post -ContentType "application/json"
      

      如果它仍然不起作用,我建议创建一个模型(数据传输对象)并使用模型绑定来绑定您的字符串值。编辑控制器 Post 方法如下:

      public class MyModelDto {
          public string Value { get; set; }
      }
      
      // POST api/loans
      [HttpPost]
      public void Post([FromBody] MyModelDto model)
      {
          string value = model.Value;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-10
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        • 2012-06-29
        • 1970-01-01
        相关资源
        最近更新 更多