Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to use HTTP redirects in a RESTful MVC framework?

I'm trying to develop an MVC framework.

When a user creates a new record it seems to make sense to me to then display the new record if it is successfully created.

Is it ok to use a http redirect to move the user to a view of the new record?

like image 347
jx12345 Avatar asked Mar 10 '12 18:03

jx12345


3 Answers

Don't do it! Use the correct HTTP response code for the situation. For example, if the user POSTs a new record to your system:

POST record/id
New record stuff

Give feedback not only as an HTML representation with a smiling happy face, but also as the correct HTTP response code.

201 Created
like image 66
supertopi Avatar answered Nov 12 '22 10:11

supertopi


You should respond a "create" action with a semantic answer (ie. HTTP 201 - Created) while displaying the newly created record, this is usually considered the norm.

Also, adding a Location header indicating the "read" action for the newly added item is even better.

You may want to take a look at the Richardson Maturity Model's view on that, there a nice article that Martin Fowler wrote about it.

like image 36
Nathan Avatar answered Nov 12 '22 11:11

Nathan


If you are creating a record then HTTP 201 is the recommended status code.

However there are certain situation when you might want to redirect. For example your api is no longer in the current url and you want to redirect users to new url.

like image 2
Shiplu Mokaddim Avatar answered Nov 12 '22 09:11

Shiplu Mokaddim