Question

I am having a very hard time understanding the exact process of "post/redirect/get".

I have combed through this site and the web for several hours and cannot find anything other than "here's the concept".

How to understand the post/redirect/get pattern?

Était-ce utile?

La solution

As you may know from your research, POST-redirect-GET looks like this:

  • The client gets a page with a form.
  • The form POSTs to the server.
  • The server performs the action, and then redirects to another page.
  • The client follows the redirect.

For example, say we have this structure of the website:

  • /posts (shows a list of posts and a link to "add post")
    • /<id> (view a particular post)
    • /create (if requested with the GET method, returns a form posting to itself; if it's a POST request, creates the post and redirects to the /<id> endpoint)

/posts itself isn't really relevant to this particular pattern, so I'll leave it out.

/posts/<id> might be implemented like this:

  • Find the post with that ID in the database.
  • Render a template with the content of that post.

/posts/create might be implemented like this:

  • If the request is a GET request:
    • Show an empty form with the target set to itself and the method set to POST.
  • If the request is a POST request:
    • Validate the fields.
    • If there are invalid fields, show the form again with errors indicated.
    • Otherwise, if all fields are valid:
      • Add the post to the database.
      • Redirect to /posts/<id> (where <id> is returned from the call to the database)

Autres conseils

Wikipedia explains this so well!

The Problem

The Problem

The Solution

The Solution

I'll try explaining it. Maybe the different perspective does the trick for you.

With PRG the browser ends up making two requests. The first request is a POST request and is typically used to modify data. The server responds with a Location header in the response and no HTML in the body. This causes the browser to be redirected to a new URL. The browser then makes a GET request to the new URL which responds with HTML content which the browser renders.

I'll try to explain why PRG should be used. The GET method is never supposed to modify data. When a user clicks a link the browser or proxy server may return a cached response and not send the request to the server; this means the data wasn't modified when you wanted it to be modified. Also, a POST request shouldn't be used to return data because if the user wants to just get a fresh copy of the data they're forced to re-execute the request which will make the server modify the data again. This is why the browser will give you that vague dialog asking you if you are sure you want to re-send the request and possibly modify data a second time or send an e-mail a second time.

PRG is a combination of POST and GET that uses each for what they are intended to be used for.

Just so people can see a code example (this is using express):

app.post('/data', function(req, res) {
  data = req.body;  //do stuff with data
  res.redirect('public/db.html');
});

So to clarify, it instantly refreshes the webpage and so on refresh of that webpage (e.g. if you updated an element on it) it won't repost the form data.

My code used to look like this:

app.post('/data', function(req, res) {
   data = req.body;
   res.sendFile('public/db.html');
});

So here the response is sending the html file at the /data address. So in the address bar, after pressing the submit button it would say for me: localhost:8080/data. But this means that on refresh of that page, if you have just submitted the form, it will submit it again. And you don't want the same form submitted twice in your database. So redirecting it to the webpage (res.redirect) instead of sending the file (res.sendFile) , stops the resubmission of that form.

It is all a matter of concept, there is no much more to understand :

  • POST is for the client to send data to the server
  • GET is for the client to request data from the server

So, conceptually, there is no sense for the server to answer with a resource data on a POST request, that's why there is a redirection to the (usually) same resource that has been created/updated. So, if POST is successful, the server opiniates that the client would want to fetch the fresh data, thus informing it to make a GET on it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top