Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are PUT and POST requests required/expected to have a request body?

I'm writting a RESTful api, and at I'm thinking about the process of a user creating a key. I have the following possibilities:

  • GET request to /new/<keyname> - although it's very easy I think I won't use this, because I heard GET is for retrieving and/or listing information;
  • POST request to /<keyname> - This seemed to me easy and simple enough, but does not pass any data in the request body. Can I do it this way ? Is this weird ?
  • POST request to /keys passing in the request body "keyname=SomeKey" - Is this the correct way ?

I looked at this API from joyent and in all their PUT and POST requests they pass some data in the request body. Is this expected ? Is it really wrong not to require a request body in a PUT and POST request ?

like image 488
João Pinto Jerónimo Avatar asked Sep 06 '11 17:09

João Pinto Jerónimo


People also ask

Is request body mandatory for PUT request?

If the request has a Content-Length header, then it has a body. It may be an empty body, but still a body. In contrast to a request with no Content-Length header, which has no body at all, not even an empty one. So yes, a PUT request, technically, strictly, has to have a body.

Can a POST request not have a body?

If you use POST /uri without a body it is something like using a function which does not take an argument . e.g int post (void); so it is reasonable to have function to your resource class which can change the state of an object without having an argument.

Does PUT request have a response body?

PUT should never return a body, but must return a response code in the header. Just choose 200 if it was successful, and 4xx if not.

Does HTTP POST request have body?

Short answer: in POST requests, values are sent in the "body" of the request. With web-forms they are most likely sent with a media type of application/x-www-form-urlencoded or multipart/form-data .


1 Answers

I asked this question on the Http-WG. This was the most precise answer I got http://lists.w3.org/Archives/Public/ietf-http-wg/2010JulSep/0276.html

In summary, POST does not require a body. I would expect the same justification can be applied to PUT.

like image 131
Darrel Miller Avatar answered Oct 01 '22 16:10

Darrel Miller