|
|
**Attention!** You must install the `http-cookie` gem to use this feature.
|
|
|
|
|
|
The cookies plugin allows to pass cookies in requests, and allow cookies to be passed to subsequent requests.
|
|
|
The `cookies` plugin seamlessly creates the concept of a stateful session, i.e. all requests flowing through it will share and update the cookie store.
|
|
|
|
|
|
A user can also pass the initial set of cookies when starting the session, though.
|
|
|
|
|
|
How to pass cookies:
|
|
|
|
... | ... | @@ -9,13 +11,20 @@ http = HTTPX.plugin(:cookies) |
|
|
http.cookies("foo" => "bar").get("https://example.com")
|
|
|
```
|
|
|
|
|
|
How to pass along cookies:
|
|
|
But this is the ideal scenario:
|
|
|
|
|
|
```ruby
|
|
|
http = HTTPX.plugin(:cookies)
|
|
|
response = http.get("https://example.com/setscookies")
|
|
|
cookies = response.cookie_jar
|
|
|
http.cookies(cookies).get("https://example.com/another")
|
|
|
response = http.get("https://example.com/setscookies") #=> "302 ... Location: https://example.com/another ... Set-Cookie: aadm...."
|
|
|
response = http.get(response.headers["location"]) => Sends the cookie information
|
|
|
```
|
|
|
|
|
|
This plugin also works seamlessly with the `follow_redirects` plugin, in the redirections setting a session cookie will be respected:
|
|
|
|
|
|
```ruby
|
|
|
http = HTTPX.plugin(:cookies, :follow_redirects)
|
|
|
response = http.get("https://example.com/setscookies") #=> will redirect to "https://example.com/another" and set the cookie
|
|
|
```
|
|
|
|
|
|
|
|
|
Next: [Compression](Compression) |
|
|
\ No newline at end of file |