0

Screenshot of Postman

I've seen this question asked a few times but with no answer which seems to help or really fit my case. Here's my code first:

So here's my code; the value is always null no matter what I do, and by adding the [System.Web.Mvc.HttpPost] decoration I now get an error stating that the route doesn't support posts. I need some help.

[System.Web.Mvc.HttpPost]
public string Post(string value)
{
    return value;
}

Here is what I'm posting via Postman; I'm putting this in the body:

"test"

No matter what I put in the body, the value of "value" is always null. I'm not sure what to do here, nothing seems to work. I even changed the decoration to just [HttpPost]. Gets work just fine for this controller, it's just the post which is failing miserably.

Richard Wolford
  • 353
  • 2
  • 15
  • 1
    But a post doesn't use body without [FromBody] attribute, I think - maybe this link? https://stackoverflow.com/questions/10984040/post-parameter-is-always-null – Nikki9696 Jan 08 '19 at 16:33
  • I've updated the signature to `public string Post([FromBody]string value)` Still the same result. – Richard Wolford Jan 08 '19 at 16:36
  • I updated my original post with a screenshot of Postman. I'm in agreement that I'm probably not passing the value correctly or something similar, but I'm at a loss as to where I'm going wrong. – Richard Wolford Jan 08 '19 at 16:46
  • 1
    That's an array of objects, with a property of "value" with a value of "test". It needs to be *just* a string. Try just sending *test* with nothing around it, not even quotes. – Reinstate Monica Cellio Jan 08 '19 at 16:48
  • Unfortunately the same thing, I get a return of null. I did set a breakpoint so the correct method is being called, but the value of "value" is still null. Talk about frustrating :( – Richard Wolford Jan 08 '19 at 16:56
  • [Postman](http://i67.tinypic.com/29p5575.png) <-- Should be a link to my Postman call – Richard Wolford Jan 08 '19 at 16:59
  • Wow, that IS frustrating. When you created the web api project, what did you select, version-wise (this isn't core, is it?)? I'm going to throw together a little test and see if it works for me. – Nikki9696 Jan 08 '19 at 17:35
  • Definitely not core, and thanks for your help. – Richard Wolford Jan 08 '19 at 17:50
  • ok, mine works but was a pain =) Will write up something in a few here. – Nikki9696 Jan 08 '19 at 17:58

2 Answers2

0

Try

public ActionResult PostName([FromBody] string value)
 {
  ...
 }

And put "value" in Body type raw in PostMan.

{
    "value" : "hi"
}
aterno
  • 21
  • 3
0

Okay, so I created a simple Web API project using Visual Studio, so it has all the default routes and whatnot.

enter image description here

Then I grabbed postman and fiddled with it, until I got it to post properly to the endpoint. Note the format header of application/json, but since we're only sending a string, it's JUST a string.

enter image description here

Now the value is here in the breakpoint

enter image description here

Nikki9696
  • 6,260
  • 1
  • 28
  • 23
  • This is exactly what I did and it epically failed for me :( I even tried a curl passing in a string and it failed. I've no idea why, I've not had this type of trouble before. However, I did get a solution in that by using data models within the code and then passing in json which corresponds to the model, I'm able to get the posts to work. So I guess this can be marked solved? – Richard Wolford Jan 08 '19 at 18:13