REST Approach: If data is being modified, is a GET better than a PUT with a 1 character body

127 Views Asked by At

We have a pair of REST applications that are trying to achieve a light version of transactions by using an acknowledgment URL that indicates that it is time for both sides to "commit" the modification. Here are the steps - these are not done instantly - there might be pauses for end user approval between the steps.

  1. Application A approves and stages the change notifies Application B that it is ready to make the change
  2. Application B stages the change and notifies Application A that it is ready and provides an acknowledgement URL that Application A will "hit" when it does the commit.
  3. Application A "hits" the acknowledgement URL, upon receipt, Application B commits the change and returns a 200. If Application A gets a 200 they commit the change otherwise everyone rolls back.

The question is what is the right operation to do with respect to the acknowledgement URL. There are two "reasonable" choices - each with disadvantages:

  • Use a GET request - The flaw is that data is being modified and that should not be allowed with a GET.

  • Use a PUT request - This makes more sense as data is changed and it
    is only suposed to happen once. But PUT requests require a
    Content-Length header and since there is no data to send, many HTTP
    client libraries refuse to set "Content-Length: 0". So they produce a "broken" PUT and you often (rightly) receive a 411 when you send a PUT with no content-item. The solution is to send a single character body - it makes the PUT valid but feels a bit yucky.

This is a narrow question asking if we should design the protocol with a GET or PUT with a single character body.

Just to be clear, not all HTTP clients refuse to send Content-Length: 0 header but the Java HttpURLConnection will only send the header if you actually send data on a PUT. And not all HTTP servers throw 411 when they get a PUT with no Content-Length header - but Rails (and others) rightly enforce the rule. We need this protocol to work across all languages, HTTP servers, and HTTP clients so an answer of "rewrite it all in X" is not helpful.

This is a REST philosophy question, not a "which language is better" question.

0

There are 0 best solutions below