Processing of Redirects

Learn to process redirects after getting a REST request.

We'll cover the following

Redirects

An HTTP redirect is sent from a server to a client in response to a request. In effect, it says, “I’m done processing this request, and you should go here to see the results.” The redirect response includes a URL that the client should try next along with some status information saying whether this redirection is permanent (status code 301 or temporary 307). Redirects are sometimes used when web pages are reorganized, and clients accessing pages in the old locations will be referred to the page’s new home. More commonly, Rails applications use redirects to pass the processing of a request off to some other action.

Redirects are handled behind the scenes by web browsers. Usually, the only way we’ll know that we’ve been redirected is a slight delay and the fact that the URL of the page we’re viewing will have changed from the one we requested. This last point is important: as far as the browser is concerned, a redirect from a server acts pretty much the same as having an end-user enter the new destination URL manually.

Redirects are actually very important when writing well-behaved web applications. Let’s look at a basic blogging application that supports comment posting. After a user has posted a comment, our application should redisplay the article with the new comment at the end.

It’s tempting to code this using logic such as the following:

Get hands-on with 1400+ tech skills courses.