简体   繁体   English

JSON 补丁 HTTP 从客户端到 API 的请求(在 AWS 上发布)返回“StatusCode:405,ReasonPhrase:'Method Not Allowed'”

[英]JSON Patch HTTP request from client to API (published on AWS) returns "StatusCode: 405, ReasonPhrase: 'Method Not Allowed'"

I have an ASP.NET Core Web API published to Amazon ECS using AWS Fargate with working PATCH request that I have successfully tested using POSTMAN. Now I am trying to make that request in the client side application by following this .我有一个 ASP.NET 核心 Web API 使用 AWS Fargate 发布到 Amazon ECS,我已经使用 POSTMAN 成功测试了工作 PATCH 请求。现在我试图通过遵循在客户端应用程序中发出该请求。

What I have on client side is this:我在客户端的是这样的:

public async Task<ActionResult> Patch(int companyId, string description)
{
    JsonPatchDocument<CompanyInfo> patchDoc = new JsonPatchDocument<CompanyInfo>();
    patchDoc.Replace(e => e.Description, description);

    var jsonSerializeObject = JsonConvert.SerializeObject(patchDoc);
    Debug.WriteLine(jsonSerializeObject);

    var method = new HttpMethod("PATCH");
    var request = new HttpRequestMessage(method, "api/CompanyInfo/" + companyId)
    {
        Content = new StringContent(jsonSerializeObject, Encoding.Unicode, "application/json")
    };


    response = await _httpClient.SendAsync(request);
    Debug.WriteLine(response);

    return RedirectToAction(nameof(Index));
}

This is what I get in my response:这是我在回复中得到的:

StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Date: Sat, 26 Nov 2022 06:06:08 GMT
  Connection: keep-alive
  Server: Kestrel
  Content-Length: 0
  Allow: GET, PUT
}

As previously mentioned I have already confirmed the following json patch document using POSTMAN:如前所述,我已经使用POSTMAN确认了以下json补丁文件:

[
    {
        "value":"some text value",
        "path":"/Description",
        "op":"replace"
    }
]

The API: API:


[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
    var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
    if (companyEntity == null)
    {
        return NotFound();
    }

    var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);

    patchDocument.ApplyTo(companyToPatch, ModelState);

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    if (!TryValidateModel(companyToPatch))
    {
        return BadRequest(ModelState);
    }

    _mapper.Map(companyToPatch, companyEntity);
    await _moviePlanetRepository.Save();

    return NoContent();
}

add [FromBody] attribute添加 [FromBody] 属性

[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, [FromBody]JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
    var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
    if (companyEntity == null)
    {
        return NotFound();
    }

    var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);

    patchDocument.ApplyTo(companyToPatch, ModelState);

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    if (!TryValidateModel(companyToPatch))
    {
        return BadRequest(ModelState);
    }

    _mapper.Map(companyToPatch, companyEntity);
    await _moviePlanetRepository.Save();

    return NoContent();
}

Had to specify that the companyId was a required for the Route.必须指定companyId是路由所必需的。 From this从此

It wasn't sufficient to provide the above attributes to the methods.向方法提供上述属性是不够的。 It was required to provide the methods' address (and query string, resp.) parameters for these attributes ([HttpPut("{id}")], [HttpDelete("{id}")]), too.还需要为这些属性([HttpPut("{id}")]、[HttpDelete("{id}")])提供方法的地址(和查询字符串,resp.)参数。 (This is particular to ASP.NET Core.) (这是 ASP.NET Core 特有的。)

[Route("{companyId:int}")]
[HttpPatch]
public async Task<ActionResult> PartiallyUpdateCompanyInfo(int companyId, [FromBody]JsonPatchDocument<CompanyInfoForPatchDto> patchDocument)
{
    var companyEntity = await _moviePlanetRepository.GetCompanyById(companyId, false);
    if (companyEntity == null)
    {
        return NotFound();
    }

    var companyToPatch = _mapper.Map<CompanyInfoForPatchDto>(companyEntity);

    patchDocument.ApplyTo(companyToPatch, ModelState);

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    if (!TryValidateModel(companyToPatch))
    {
        return BadRequest(ModelState);
    }

    _mapper.Map(companyToPatch, companyEntity);
    await _moviePlanetRepository.Save();

    return NoContent();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ADT RestAPI 的 POST 命令返回“HTTP/1.1 405 Method Not Allowed Allow: DELETE,GET,PATCH,PUT Content-Length:0” - POST Command of ADT RestAPI returns "HTTP/1.1 405 Method Not Allowed Allow: DELETE,GET,PATCH,PUT Content-Length:0" Microsoft Graph 更新 App Registration Approles API 补丁方法总是返回无法读取 JSON 请求负载 - Update App Registration Approles by Microsoft Graph API Patch method returns always Unable to read JSON request payload AWS Cognito oauth2/token 端点中不允许出现 405 方法错误 - 405 method not allowed error in AWS Cognito oauth2/token endpoint 从 HTML 向 AWS API 网关提交 HTTP 请求 - Submitting HTTP request from HTML to AWS API gateway 如何在我的 AWS REST API 访问日志中查看完整的 HTTP 请求(标头、方法、正文)? - How can I see the full HTTP request (headers, method, body) in my AWS REST API access logs? 登录 /store 后 WSO2 api 管理器失败(405 - 方法不允许) - WSO2 api manager fails (405 - Method Not Allowed) after signing-in to /store AWS cloudwatch API 金丝雀状态代码失败,即使记录(记录在 cloudwatch 日志中)状态代码为 200 - AWS cloudwatch API canary statuscode comes as failed even the logged(logged in cloudwatch logs) statusCode is 200 AWS Api Gateway Lambda代理集成,如何从客户端请求中获取源端口 - AWS Api Gateway Lambda proxy integration, how to get source port from client request AWS API 网关 HTTP API CORS 预检请求 - AWS API Gateway HTTP API CORS preflight request 如何使用 Lambda 访问 AWS API Gateway 请求的 HTTP 标头? - How to access HTTP headers for request to AWS API Gateway using Lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM