Calling POST method from API controller

16,286

Solution 1

Create a class that corresponds to your JSON:

public class Test
{
    public string value{get; set;}
    public int ID {get; set;}
}

And then change your Api-action:

// POST api/myfiles
public void Post([FromBody]Test value)
{

}

If you don't want to do that, just change the POST-body:

"somevalue"

EDIT: Added ID to the POST-payload. Now your JSON should look like this:

{"value": "someval",
"ID": 1}

Solution 2

According to the MVC document http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1, Which exactly shows the same case as yours

Go to the Sending Simple Types part.

When you sending simple value, you need to

  1. Use FromBody Attribute

  2. the client needs to send the value with the following format:

    =value

    Specifically, the name portion of the name/value pair must be empty for a simple type.

Anyway, for further enhancement, you'd better use a complex type (an object) to accept the parameter.

Solution 3

You need to change from:

Request body:
{"value": "asjkfsf"}

To:

Request body:
"asjkfsf"

It will work for simple type with json format

Share:
16,286
petko_stankoski
Author by

petko_stankoski

Updated on June 08, 2022

Comments

  • petko_stankoski
    petko_stankoski almost 2 years

    I have an API controller named MyFIlesController.

    In it, I have this method:

    // POST api/myfiles
    public void Post([FromBody]string value)
    {
    }
    

    And here's how I call it with Fiddler:

    POST
    URL: `http://localhost:58075/api/myfiles`
    
    -------------------------
    
    Request Header:
    
    User-Agent: Fiddler
    
    Host: localhost:58075
    
    Content-Type: application/json
    
    Content-length: 18
    
    -------------
    
    Request body:
    {"value": "asjkfsf"}
    

    The method gets called, but value is null. What am I doing wrong?

    • J0e3gan
      J0e3gan almost 11 years
      Are you showing a Fiddler-logged request that your code made or a request that you built in Fiddler?
    • graumanoz
      graumanoz almost 11 years
      Add [HttpPost] attribute to this method
    • petko_stankoski
      petko_stankoski almost 11 years
      @J0e3gan A request that I built in Fiddler.
    • Tommi
      Tommi almost 11 years
      '{"value": "asjkfsf"}'.length -> 20; not probably the reason of issue, but nevertheless.
    • J0e3gan
      J0e3gan almost 11 years
      So it sounds like the question is why Post's caller is passing null for value, correct?
    • petko_stankoski
      petko_stankoski almost 11 years
      @Tommi The content length should be the length of {"value": "asjkfsf"} string?
    • Tommi
      Tommi almost 11 years
      AFAIK Content-Length should be your whole request body length.
  • petko_stankoski
    petko_stankoski almost 11 years
    And what if the method has two input parameters? ([FromBody]string value, int id)
  • petko_stankoski
    petko_stankoski almost 11 years
    And what if the method has two input parameters? ([FromBody]string value, int id)
  • cuongle
    cuongle almost 11 years
    @petko_stankoski: if you use id for just query, put it on querystring ?id=.... For more, you really need a model
  • Kenneth
    Kenneth almost 11 years
    Do you want the id inside the JSON or as a Url-part?
  • petko_stankoski
    petko_stankoski almost 11 years
    How can I use ?id= for POST method?
  • petko_stankoski
    petko_stankoski almost 11 years
    I want the id to be in the Request Body.
  • cuongle
    cuongle almost 11 years
    @petko_stankoski: just put on URL URL: http://localhost:58075/api/myfiles?id=...
  • petko_stankoski
    petko_stankoski almost 11 years
    Hmm so there isn't a way without craeting the new class?
  • Kenneth
    Kenneth almost 11 years
    No, there isn't, unless you're willing to pass it in the URL. Then you can just add the parameter to the action method and it will just work. But not through JSON no