REST and redirecting the response

http-requesthttp-responserest

I'm developing a RESTful service. Here is a map of the current feature set:

POST /api/document/file.jpg    (creates the resource)
GET /api/document/file.jpg     (retrieves the resource)
DELETE /api/document/file.jpg  (removes the resource)

So far, it does everything you might expect. I have a particular use case where I need to set up the browser to send a POST request using the multipart/form-data encoding for the document upload but when it is completed I want to redirect them back to the form. I know how to do a redirect, but I'm not certain about how the client and server should negotiate this behavior. Two approaches I'm considering:

  1. On the server check for the multipart/form-data encoding and, if present, redirect to the referrer when the request is complete.
  2. Add a service URI of /api/document/file.jpg/redirect to redirect to the referrer when the request is complete.

I looked into setting an X header (X-myapp-redirect) but you can't tell the browser which headers to use like this. I manage the code for both the client and the server side so I'm flexible on solutions here. Is there a best practice to follow here?

Best Answer

This sounds like a pretty classic case of POST/Redirect/GET, and is sort of the expected behavior for web applications. I'd probably make that the default.

If you're not a fan of that approach for whatever reason, consider using AJAX (which means you can use the PUT approach mentioned by @sdg in a comment) with a standard 201 and the response body can contain the flash message to be displayed. The AJAX can do what you need it to do with the flash message, at that point.