Add Cookies docs

This commit is contained in:
Tom Christie 2019-06-13 14:03:22 +01:00
parent f296e6ded9
commit d5d726f66a

View File

@ -11,10 +11,10 @@ First start by importing HTTP3:
>>> import http3
```
Now, lets try to get a webpage. For this example, lets get GitHubs public timeline:
Now, lets try to get a webpage.
```python
>>> r = http3.get('https://api.github.com/events')
>>> r = http3.get('https://httpbin.org/get')
```
Similarly, to make an HTTP POST request:
@ -268,3 +268,34 @@ Multiple values for a single response header are represented as a single comma s
value, as per [RFC 7230](https://tools.ietf.org/html/rfc7230#section-3.2):
> A recipient MAY combine multiple header fields with the same field name into one “field-name: field-value” pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma.
## Cookies
Any cookies that are set on the response can be easily accessed:
```python
>>> r = http3.get('http://httpbin.org/cookies/set?chocolate=chip', allow_redirects=False)
>>> r.cookies['chocolate']
'chip'
```
To include cookies in an outgoing request, use the `cookies` parameter:
```python
>>> cookies = {"peanut": "butter"}
>>> r = http3.get('http://httpbin.org/cookies', cookies=cookies)
>>> r.json()
{'cookies': {'peanut': 'butter'}}
```
Cookies are returned in a `Cookies` instance, which is a dict-like data structure
but with additional API for accessing cookies by their domain or path.
```python
>>> cookies = http3.Cookies()
>>> cookies.set('cookie_on_domain', 'hello, there!', domain='httpbin.org')
>>> cookies.set('cookie_off_domain', 'nope.', domain='example.org')
>>> r = http3.get('http://httpbin.org/cookies', cookies=cookies)
>>> r.json()
{'cookies': {'cookie_on_domain': 'hello, there!'}}
```