How can I attach some parameters with request?

What are the pros and cons of different ways of passing HTTP POST parameters to a REST API?

  • Pros and cons of: PassingĀ  in individual parameters as HTTP form parameters Passing in JSON as the request body Passing in JSON as a form parameters

  • Answer:

    I think the best choice is JSON as request body. It is better than form-encoded parameters mainly for two reasons: it has the same format that you receive when performing a GET operation it can better define data structures (embedded elements) Moreover you can use a custom Media Type (via Content Type header), one of the core elements of REST definition (see representation metadata in Fielding's dissertation).

Michele Zonca at Quora Visit the source

Was this solution helpful to you?

Other answers

TL;DR - JSON as HTTP Post Body is the best way. Three options in the question demonstrating three ways to encode data. We can encode (flat) data as form parameters (1), we can encode data as JSON (2) and then we can do both (3). Obviously double-encoding data makes no sense, so 'Passing in JSON as form parameters' is out. Comparing (1) and (2) I would prefer simpler solution and it is 'passing in JSON as the request body'. It is not only simpler, but also have better support for nested data. Additionally to that I would like to add that it is important to realize that that POST is not idempotent (see http://en.wikipedia.org/wiki/Idempotence). One should see POST as a HTTP Verb addresses do a particular resource, and in case of failure re-POST is not expected or better to say failures should be handled with extreme care by POST requests.

Renat Zubairov

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.