5

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?

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
jx12345
  • 1,650
  • 2
  • 22
  • 40

3 Answers3

6

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
supertopi
  • 3,469
  • 26
  • 38
  • Only partially correct. If record's `id` is known beforehand to the User-Agent (UA), the correct method to use (when technically possible) is PUT. If the `id` is not known by the UA and is assigned server-side, the server should respond with 201 response code and use the `Location` header to indicate URI of the newly created resource to the UA. (I wanted to clarify this, since OP specifically asked about redirects.) – MicE Mar 10 '12 at 21:27
  • Since my original post i've been looking at a few existing frameworks for inspiration and i've found that the Yii framework seems to do just what i originally suggested.. – jx12345 Mar 13 '12 at 20:14
  • @jx12345 I guess Yii isn't very ReSTful then; or at least not using http to its full extent. – supertopi Mar 13 '12 at 20:37
  • I've found that the Yii framework does just this, ie. redirect on a succesful create. The code the createAction() method of a generated controller is as follows: if(isset($_POST['Employee'])) { $model->attributes=$_POST['Employee']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } } and in firebug i get the following: POST create 302 found I'm not saying that means it's right - I just wondered if you had any thoughts on that. – jx12345 Mar 13 '12 at 20:37
  • Yeah, i suppose not... thanks again -- sorry about double posting the comments - i got distracted then got caught out with only being able to edit the comments for 5 mins – jx12345 Mar 13 '12 at 20:39
2

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.

Nathan
  • 4,017
  • 2
  • 25
  • 20
2

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.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • well thats kind of what i found, i was creating the thing in the database then using the existing 'view' that i used to just view a thing but then any relative links would be broke beacuase i would be at example.com/create rather than where i ought to be ie. example.com/view/1 so i decided to use a redirect but i kind of felt wrong. – jx12345 Mar 13 '12 at 20:58