Follow or stop at redirects
For Rust developers who need control over where a request ends up
You follow a chain of three redirects, put a limit on how many a client will follow and turn redirects off so you can read the Location header yourself.
Before you start
You need Rust with Cargo. fibre_http comes from crates.io:
fibre_http = "0"Set up the project#
Start a new binary crate.
Put this in the [dependencies] section of Cargo.toml:
[]
= "0"The program#
httpbin.org's /redirect/3 sends you through three redirects before it answers. Replace src/main.rs with this:
use ;
Run it:
What each part does#
A new client follows up to 10 redirects, so the first request ends on the final page.
redirects(2) lowers the limit. Three redirects is one too many, so send returns Error::TooManyRedirects.
redirects(0) turns following off. You get the first redirect back as a normal response and can decide what to do with its Location header.
When the client follows a redirect it behaves the way a browser does. A 301 or 302 turns a POST into a GET, while a 307 or 308 keeps the method and the body. A redirect to a different site drops the Authorization and Cookie headers, so your credentials are never sent somewhere you did not ask for.
Where to go next#
The usage guide has the full redirect rules: https://github.com/excsn/fibre/blob/main/http/README.USAGE.md
