How to post to MVC endpoint via postman with JSON object

折月煮酒 提交于 2021-01-28 07:05:58

问题


I have a JSON object and trying to have a post request to my API endpoint.

In my Listing controller I have the following function

[HttpPost]
public async Task<IActionResult> Import(GetImportInput input)
{

    return StatusCode(200);
}

GetImportInput.cs

public string Name {get; set;}

Postman details:

ContentType = application/json

Body = {
            "name" : "Rabbit"
       }

When I put a breakpoint inside my Import method, the breakpoint hits, but the parameter input does not have the value Rabbit. May I ask how do I properly get my postman to send the body so my controller method will pick it up.

Header Tab


回答1:


You are missing [FromBody] in your controller method, casing is not an issue here. Remember to use header Content-Type: application/json when testing this with Postman.

public class ListingController : ControllerBase
{
    [HttpPost]
    public IActionResult Import([FromBody]GetImportInput input)
    {
        return StatusCode(200);
    }
}


来源:https://stackoverflow.com/questions/63349661/how-to-post-to-mvc-endpoint-via-postman-with-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!