Skip to content

Post/Redirect/Get

Reflection on Design

We have two endpoints with the same path (/courses); note, however, one endpoint is for HTTP Post and the other is for Get request. This design is based on a pattern called Post/Redirect/Get, or PRG.

Quote

Post/Redirect/Get (PRG) is a web development design pattern that lets the page shown after a form submission be reloaded, shared, or bookmarked without ill effects, such as submitting the form another time. - Wikipedia

After a web form is submitted to a server through an HTTP Post request, if a client attempts to refresh the page, it may trigger a Request/Response loop that can cause the contents of the original Post to be resubmitted.

To avoid this problem, we use the PRG pattern; instead of returning a web page directly, the Post response returns a redirect that triggers a Get request:

1
2
3
4
5
6
7
8
9
get("/courses", (req, res) -> {
    // Respond to Get request
});

post("/courses", (req, res) -> {
    // Respond to Post request, and then
    res.redirect("/courses");
    return null;
});