quickstart.md
This file
Path
docs/quickstart.md
270 lines · 6.5 KiB · Markdown
Last changed by
http3 (#86)Tom Christiebefore adoptionc9747aa
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271# QuickStart
!!! note This page closely follows the layout of the `requests` QuickStart documentation. The `http3` library is designed to be API compatible with `requests` wherever possible.
First start by importing HTTP3:
```>>> import http3```
Now, let’s try to get a webpage. For this example, let’s get GitHub’s public timeline:
```python>>> r = http3.get('https://api.github.com/events')```
Similarly, to make an HTTP POST request:
```python>>> r = http3.post('https://httpbin.org/post', data={'key': 'value'})```
The PUT, DELETE, HEAD, and OPTIONS requests all follow the same style:
```python>>> r = http3.put('https://httpbin.org/put', data={'key': 'value'})>>> r = http3.delete('https://httpbin.org/delete')>>> r = http3.head('https://httpbin.org/get')>>> r = http3.options('https://httpbin.org/get')```
## Passing Parameters in URLs
To include URL query parameters in the request, use the `params` keyword:
```python>>> params = {'key1': 'value1', 'key2': 'value2'}>>> r = http3.get('https://httpbin.org/get', params=params)```
To see how the values get encoding into the URL string, we can inspect theresulting URL that was used to make the request:
```python>>> r.urlURL('https://httpbin.org/get?key2=value2&key1=value1')```
You can also pass a list of items as a value:
```python>>> params = {'key1': 'value1', 'key2': ['value2', 'value3']}>>> r = http3.get('https://httpbin.org/get', params=params)>>> r.urlURL('https://httpbin.org/get?key1=value1&key2=value2&key2=value3')```
## Response Content
HTTP3 will automatically handle decoding the response content into unicode text.
```python>>> r = http3.get('https://www.example.org/')>>> r.text'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'```
You can inspect what encoding has been used to decode the response.
```python>>> r.encoding'UTF-8'```
If you need to override the standard behavior and explicitly set the encoding touse, then you can do that too.
```python>>> r.encoding = 'ISO-8859-1'```
## Binary Response Content
The response content can also be accessed as bytes, for non-text responses:
```python>>> r.contentb'<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>...'```
Any `gzip` and `deflate` HTTP response encodings will automaticallybe decoded for you. If `brotlipy` is installed, then the `brotli` responseencoding will also be supported.
For example, to create an image from binary data returned by a request, you can use the following code:
```python>>> from PIL import Image>>> from io import BytesIO>>> i = Image.open(BytesIO(r.content))```
## JSON Response Content
Often Web API responses will be encoded as JSON.
```python>>> r = http3.get('https://api.github.com/events')>>> r.json()[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...' ... }}]```
## Custom Headers
To include additional headers in the outgoing request, use the `headers` keyword argument:
```python>>> url = 'http://httpbin.org/headers'>>> headers = {'user-agent': 'my-app/0.0.1'}>>> r = http3.get(url, headers=headers)```
## Sending Form Encoded Data
Some types of HTTP requests, such as `POST` and `PUT` requests, can include datain the request body. One common way of including that is as form encoded data,which is used for HTML forms.
```python>>> data = {'key1': 'value1', 'key2': 'value2'}>>> r = http3.post("https://httpbin.org/post", data=data)>>> print(r.text){ ... "form": { "key2": "value2", "key1": "value1" }, ...}```
Form encoded data can also include multiple values form a given key.
```python>>> data = {'key1': ['value1', 'value2']}>>> r = http3.post("https://httpbin.org/post", data=data)>>> print(r.text){ ... "form": { "key1": [ "value1", "value2" ] }, ...}```
## Sending JSON Encoded Data
Form encoded data is okay if all you need is simple key-value data structure.For more complicated data structures you'll often want to use JSON encoding instead.
```python>>> data = {'integer': 123, 'boolean': True, 'list': ['a', 'b', 'c']}>>> r = http3.post("https://httpbin.org/post", json=data)>>> print(r.text){ ... "json": { "boolean": true, "integer": 123, "list": [ "a", "b", "c" ] }, ...}```
## Sending Binary Request Data
For other encodings you should use either a `bytes` type, or a generatorthat yields `bytes`.
You'll probably also want to set a custom `Content-Type` header when uploadingbinary data.
## Response Status Codes
We can inspect the HTTP status code of the response:
```python>>> r = http3.get('https://httpbin.org/get')>>> r.status_code<StatusCode.OK: 200>```
The status code is an integer enum, meaning that the Python representation givesuse some descriptive information, but the value itself can be used as a regular integer.
```python>>> r.status_code == 200True```
HTTP3 also includes an easy shortcut for accessing status codes by their text phrase.
```python>>> r.status_code == requests.codes.OKTrue```
We can raise an exception for any Client or Server error responses (4xx or 5xx status codes):
```python>>> not_found = http3.get('https://httpbin.org/status/404')>>> not_found.status_code<StatusCode.NOT_FOUND: 404>>>> not_found.raise_for_status()Traceback (most recent call last): File "/Users/tomchristie/GitHub/encode/httpcore/http3/models.py", line 776, in raise_for_status raise HttpError(message)http3.exceptions.HttpError: 404 Not Found```
Any successful response codes will simply return `None` rather than raising an exception.
``` python>>> r.raise_for_status()```
## Response Headers
The response headers are available as a dictionary-like interface.
```python>>> r.headersHeaders({ 'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'connection': 'close', 'server': 'nginx/1.0.4', 'x-runtime': '148ms', 'etag': '"e1ca502697e5c9317743dc078f67693f"', 'content-type': 'application/json'})```
The `Headers` data type is case-insensitive, so you can use any capitalization.
```python>>> r.headers['Content-Type']'application/json'
>>> r.headers.get('content-type')'application/json'```
Multiple values for a single response header are represented as a single comma separatedvalue, 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.