Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there a preferred/recommended Internet Media Type for RESTful POST requests?

For example, if I want to add a new user, should I send the data as a URL encoded string like:

name=Foo&otherData=Bar

or does REST recommend I send the data as whatever format the response is going to be sent as (e.g. JSON/XML)?

<user>
  <name>Foo</name>
  <other-data>Bar</other-data>
</user>
share|improve this question
add comment

3 Answers 3

up vote 7 down vote accepted

Typically web services support both XML and JSON based on the user's preference. A lot of people tend to use JSON because it usually allow you to send the same data in a more compact way, which is important for web services(especially if they have mobile clients).

share|improve this answer
add comment

If you can choose, use JSON. It's simple, readable, easily handled both in the server, client or browser's JavaScript, and quick to parse. And is in effect the informal standard for REST. Although XML is usually supported too by most frameworks, there's no reason to use it unless you're somehow forced to. But a URL-like string is definitely not a normal encoding for POST data in REST.

share|improve this answer
add comment

Is there a preferred/recommended Internet Media Type for RESTful POST requests?

No there isn't. Use the media type that is most convenient for the clients that need to access the API.

With that said, you should always strive to use well-standardized formats, like XML or JSON or application/x-www-form-urlencoded to reduce the amount of work needed in the client. Additionally, if the API is meant for use outside your organization, you'd want to accept requests in multiple media types to let the clients choose the one that's most convenient for them; require the client to send Content-Type header in their requests so the server know how to decode the request body.

or does REST recommend I send the data as whatever format the response is going to be sent as (e.g. JSON/XML)?

There isn't any requirement for request body to have the same content type as the response, nor any need to. A REST client should send a Content-Type header to specify the request's media type and Accept header to negotiate the response media type.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.