liorblob
liorblob

Reputation: 537

ASP .NET Web API - Get plain json in POST method

I have a Web API controller that has a POST method which receives data from the request body, and all it does is send it to another web service, so deserializing the data is not necessary. How can I disable the auto deserialization done by the Web API?

public IHttpActionResult Post([FromBody]string data)
{
 //Post with http client...

}

The data arrives as null with this signature.

Upvotes: 0

Views: 3706

Answers (3)

zhimin
zhimin

Reputation: 3050

Just read the reuest content like this:

public Task<IHttpActionResult> Post() {
    var str = await Request.Content.ReadAsStringAsync();
}

Upvotes: 0

owczarek
owczarek

Reputation: 347

Check this:

public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
    var data = await request.Content.ReadAsStringAsync();
    // do stuff with the content
}

More about the solution: http://bizcoder.com/posting-raw-json-to-web-api

Upvotes: 1

Mokhtar Ashour
Mokhtar Ashour

Reputation: 600

Send the object as string (in its serialized format). For example

YourMethod([FromBody]string json)

Upvotes: 0

Related Questions