代码之家  ›  专栏  ›  技术社区  ›  BigPino

带有json正文的HTTPful请求返回错误400

  •  0
  • BigPino  · 技术社区  · 6 年前

    我有这个密码:

    $arr = array("id" => 3);
    
    $response = \Httpful\Request::get($restapiUrl)
                ->sendsJson()
                ->expectsJson()
                ->body(json_encode($arr))
                ->send();
    

    每次调用我的API都以失败告终,错误404:请求错误。我必须通过php的请求体发送参数。

    我的api代码:

    [HttpGet]
    [Route("api/MyController/GetProduct")]
    public IHttpActionResult GetProduct([FromBody] int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }
    

    我错过了什么?

    0 回复  |  直到 6 年前