5

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"
       }

c#

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

enter image description here

5
  • Try Name instead of name in the JSON. Commented Aug 11, 2020 at 0:14
  • @ADyson name is still null, unfortunately. Commented Aug 11, 2020 at 0:31
  • Ok. as well as that please also try public async Task<IActionResult> Import([FromBody] GetImportInput input) (see andrewlock.net/model-binding-json-posts-in-asp-net-core and stackoverflow.com/a/45086673/5947043 and others) Commented Aug 11, 2020 at 0:34
  • Can you show the headers tab? Commented Aug 11, 2020 at 0:50
  • @NoahStahl posted Commented Aug 11, 2020 at 1:19

1 Answer 1

4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.