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?
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With