3

I'm building my first WebAPI using ASP.NET MVC 4 WebAPI.

The requests must be sent using the application/json ContentType with utf-8 as the character set.

My POST method looks like this:

    public HttpResponseMessage Post([FromBody]string value)
    {
        return new HttpResponseMessage(HttpStatusCode.OK);
    }

Whenever I'm sending a POST request the parameter 'value' is null. The request body contains Json: { "name":"test" }.

What I prefer is to have the parameter of the Post method to be either a string containing the Json or be of type JObject (from the JSON.NET library). How do I accomplish this? And is this even possible?

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Grant
  • 73
  • 1
  • 4
  • 1
    Of course. First of all, rename value to 'name' – Andrei Drynov May 14 '13 at 11:10
  • That doesn't work. The value of the parameter is still null. The Json will eventually be something like { "name1": [ {}, {} ], "name2": [], "name3":"some value" }. I don't want the Json to be deserialized into an object by the system itself. I want to do that myself. – Grant May 14 '13 at 11:24
  • If you want to controls deserialization, add a Custom media type formatter for JSON. Otherwise, is it an option for you to send the body as x-www-form-urlencoded string instead and then to create an object from that? – Joanna Derks May 14 '13 at 11:29
  • If you are sending a JSON object to the Web API service, you can create a POCO model containing "name1, "name2", etc., then you can change your method as public HttpResponseMessage Post(YourObject object) {} After that you can deserialize it in the method using the usual JSON.NET parse method. Are you sending your JSON from JavaScript, can you show the code? – Andrei Drynov May 14 '13 at 11:29
  • Here's a good example: http://stackoverflow.com/questions/13870161/sending-json-object-to-web-api – Andrei Drynov May 14 '13 at 11:32
  • Joanna, when adding a custom media type formatter for Json, am I still able to use the application/json content-type? I found a tutorial on media type formatters here: http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters. Is this what you mean? – Grant May 14 '13 at 12:09
  • ADNow, yes that's also a solution. I'll see which one I go with. Thanks! I'm using Fiddler or Firefox Poster to send a request. – Grant May 14 '13 at 12:15

1 Answers1

2

The easiest way is to grab the raw string directly from Request.Content:

public async Task<HttpResponseMessage> Post()
{
    string value = await Request.Content.ReadAsStringAsync();

    return new HttpResponseMessage(HttpStatusCode.OK);
}

There is a way to make ASP.NET Web Api treat your request body as string content, but in order to do that the content must be in =value format, in your case something like this:

={ "name":"test" }

You can achieve something like this with following jQuery code (for example):

$.post('api/values', '=' + JSON.stringify({ name: 'test' }));

In that case you can use signature from your question.

In the end there is always an option of creating your own MediaTypeFormatter to replace the default JsonMediaTypeFormatter and make it always deserialize the content into JObject. You can read more about creating and registering MediaTypeFormatter here and here.

tpeczek
  • 23,867
  • 3
  • 74
  • 77