0

I've been trying to pass string data using [FromBody] but it is always null.

enter image description here

public HttpResponseMessage Post(int id, [FromBody] string value)
{
    return Request.CreateResponse(HttpStatusCode.OK, "Success");
}

enter image description here

Rahul Sharma
  • 6,339
  • 2
  • 23
  • 41
user3035024
  • 191
  • 2
  • 4
  • 15
  • Please post your code as `code-formatted text`, not screenshot. Would be nice if the request is posted in text form as well – Luke Vo Apr 04 '22 at 02:02

2 Answers2

2

In ASP.NET Core you cannot simply use Model binding to get the whole JSON body text as string. You have to either create a binding model or read the request body manually:

var bodyText = await this.Request.Content.ReadAsStringAsync();
Luke Vo
  • 14,925
  • 19
  • 92
  • 153
1
[HttpPost]
public HttpResponseMessage Post(int id, [FromBody] string value)
{
    return Request.CreateResponse(HttpStatusCode.OK, "Success");
}

The [HttpPost] attribute tells the routing engine to send any POST requests to that action method to the one method over the other. This is a type of overloading.enter link description here

biksh49
  • 21
  • 3