Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a POST request render HTML or redirect?

Tags:

http

post

Should a POST request render HTML or redirect?

I hate it when your on a page and refresh and get the browser telling you, you're going to post data again.

like image 885
ThomasReggi Avatar asked Aug 28 '12 01:08

ThomasReggi


1 Answers

Yes. It should send an entity or redirect!

(Sorry, the old programming jokes have to come out sometimes).

It really depends on whether you can meaningfully give something to GET, that makes sense standing on it's own.

Example: I buy something, I get a page saying "thank you, yadda yadda order number, receipt, yadda".

That should be a 303 See Other redirect, so that I GET a page with that info. I can bookmark it for later, refreshing just refreshes the GET. Happy days.

There are times though, when it only makes sense to render an immediate response, and if they refresh, then to repeat the actual operation, and bookmarking is meaningless That should not be a redirect.

For the most part, aim to have as few of the latter anyway. It is though most useful if you have to return them to the form because something failed - nobody wants a bookmark of a failed form, they want to fix what needs fixing and get on with it.

Note, many server-side systems (ASP etc) use 302 when you redirect from a POST, which strictly should mean that it POSTs again, but just about no browser does. Instead, be clearer:

  1. If you want to redirect the POST again, so the POST goes to a different URI - well don't, that has other issues - but if you really really have to, then 307
  2. If you want to follow up a POST with a GET to something explaining the result, the 303. It unambiguously means "now do a GET".
like image 194
Jon Hanna Avatar answered Oct 10 '22 07:10

Jon Hanna