Understanding the "Post/Redirect/Get" Pattern

Understanding the post/redirect/get pattern

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)

How do I implement the Post/Redirect/Get pattern in asp.net WebForms?

If the fields POSTed are being persisted before the Redirect and you need to access that data after the Redirect, I would append the identifier for the data record(s) on the query string as you mention. You could also specify a status for the request (for displaying messages etc). Then on the GET page you can read out the data and do whatever with it.

I don't see any other way to get around this as each page obtained by GET will not have access to the previous page's ViewState etc.

Using Server.Transfer will have the same effect as handling the POST on the original page.

You could use Session variables to store the POST data, but that stinks.

Trying to understand the Post/Redirect/Get design pattern (implemented with PHP)

Post Redirect Get comes in to play to stop a user resending their POST data if they refresh the page they've been redirected to after submitting a form. When you want to implement a PRG, you should set the HTTP header code to 303 like this:

header('Location: level1.php', 303);

Post/Redirect/Get pattern in flask

It can't pass anything on a redirect, as it is a completely new request.

I am confused about PHP Post/Redirect/Get

However, couldn't a user just navigate to the URL above?

Yes, he can. This will not cause anything bad though.

Also, what is the purpose of the GET variable?

To have some flag that represents the fact that the form has been processed successfully and you need to congratulate user.

Couldn't I just have a script at form.php that checks if the user is logged in?

Uhm, you can keep your code in the way you like. There is no any strong requirements

At submit.php, should I save the logged in username as $_SESSION['username'], and then check if isset() at form.php?

If you need to persist it across the current session - yes, do so.

Also, since a URL with "success" in it isn't really pretty, is it economical to redirect the user once again?

Redirect where. Redirection is pretty cheap thing.

Should I use PHP header() or Javascript window.location.href?

You definitely should do that in php, otherwise you'll get the troubles you're trying to avoid following PRG-way.

transform Post request using the Post-Redirect-Get pattern in java spring

You need to add another method that can handle a GET request for the same url mapping.
So in your POST method you just make a redirection and in your GET method you can do all the business process.

@Controller
@RequestMapping("/bus/topologie")
public class TopologieController {
private static final String VIEW_TOPOLOGIE = "topologie";

@RequestMapping(method = RequestMethod.POST, params = { "genererCle" })
public String genererClePost(final Topologie topologie, final RedirectAttributes redirectAttributes, @RequestParam("genererCle") final String genererCle, final Model model)
throws IOException {
redirectAttributes.addAttribute("genererCle", genererCle);
return "redirect:/bus/topologie";
}

@RequestMapping(method = RequestMethod.GET, params = { "genererCle" })
public String genererCleGet(final Topologie topologie, final Model model)
throws IOException {
cadreService.genererCle(topologie);
return VIEW_TOPOLOGIE;
}

How are server side errors handled in Post/Redirect/Get pattern?

I typically do it the first way you describe—redirect only in the event of a successful submission. It's hard to see a real use case for bookmarking a form containing invalid data; on the other hand it often makes sense to bookmark a confirmation page (after successful submit).

post/redirect/get

The request alone isn't a problem, especially that the alternative gives a pretty bad user experience.

However, when using a site with load balancing and/or database replication, you need to take care to ensure that the GET after POST will see the data that has been posted.

When using load balancing and caching, this is sometimes solved with "sticky sessions" that direct the same user to the same machine, so data stored in a write-through cache on that machine will be current.

When using database replication, GET requests after POST may need to read directly from the "primary" database, instead of a local "secondary" as usual.



Related Topics



Leave a reply



Submit