Keep cookies between requests
For Rust developers talking to a site that logs you in with a cookie
You turn on a client's cookie store so a cookie one response sets is sent on the requests after it, the way a browser keeps you logged in.
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", = ["cookies", "json"] }
= "1"The cookies feature adds the cookie store. The json feature is only there to read httpbin's answer.
The program#
httpbin.org's /cookies/set answers with a Set-Cookie header. Its /cookies endpoint shows which cookies your request carried. Replace src/main.rs with this:
use SyncClient;
use Value;
Run it:
What each part does#
A plain client ignores Set-Cookie, so the second request goes out with no cookies.
cookie_store(true) gives the client an in-memory store. It saves every cookie a response sets, including responses to redirects it follows on the way. Later requests carry the cookies that match their domain and path.
The store lives as long as the client and the clients cloned from it. If you set a Cookie header on a request yourself, that request keeps yours.
Where to go next#
The usage guide shows a login form posted with form and a page fetched after it: https://github.com/excsn/fibre/blob/main/http/README.USAGE.md
