0

I'm developing an api using .NET Mvc Web Api. It will accept POST requests with JSON as the payload. If I want to use the FromBody method to retrieve the POST data, the payload has to begin with an equals sign (=). (That makes it a key-value-pair with a nameless key.)

Too me this seems a bit strange, and I'm wondering if this syntax is somewhat accepted, or simply a Web Api quirk that I should avoid exposing to my consumers?

MEMark
  • 1,493
  • 2
  • 22
  • 32

1 Answers1

1

You should be able to post JSON to a web API action without beginning it with "=". It is difficult to know what is going on without seeing some code. The first thing that I can think of that might be wrong is that the Content-type header is not being set correctly. It should be

Content-type: application/json; charset=utf-8

I'm guessing the content-type is being set to

Content-type: application/x-www-form-urlencoded

or

Content-type: multipart/form-data
Daniel Auger
  • 12,535
  • 5
  • 52
  • 73
  • As I understand it this is by design. Look here for example: http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-1#sending_simple_types My question is really if it's acceptable to expose this to consumers. OTOH, if you're saying it's not even an issue in Web Api, then my question is irrelevant. – MEMark Aug 28 '14 at 08:07
  • What's posting to the method? Is it a web form in an HTML page, an AJAX request, other systems? – Daniel Auger Aug 28 '14 at 12:58
  • 1
    The example you linked to shows how to submit a form as application/x-www-form-urlencoded. You can pass a JSON string as a form key / value pair (which is why you need the "="), but the preferred way to do it would be to create a JSON object and post it as application/json instead of application/x-www-form-urlencoded. The accepted answer here might point you in the right direction: http://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object – Daniel Auger Aug 28 '14 at 13:07
  • Thanks, your answers combined made me understand! – MEMark Aug 28 '14 at 16:04