Skip to content
Merged

Docs #103

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions docs/requests.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,97 @@
# Requests

The core elements of an HTTP request are the `method`, `url`, `headers` and `body`.

```python
>>> req = httpx.Request('GET', 'https://www.example.com/')
>>> req
<Request [GET 'https://www.example.com/']>
>>> req.method
'GET'
>>> req.url
<URL 'https://www.example.com/'>
>>> req.headers
<Headers {'Host': 'www.example.com'}>
>>> req.body
b''
```

## Working with the request headers

The following headers have automatic behavior with `Requests` instances...

* `Host` - A `Host` header must always be included on a request. This header is automatically populated from the `url`, using the `url.netloc` property.
* `Content-Length` - Requests including a request body must always include either a `Content-Length` header or a `Transfer-Encoding: chunked` header. This header is automatically populated if `content` is not `None` and the content is a known size.
* `Transfer-Encoding` - Requests automatically include a `Transfer-Encoding: chunked` header if `content` is not `None` and the content is an unkwown size.
* `Content-Type` - Requests automatically include a `Content-Type` header if `content` is set using the [Content Type] API.

## Working with the request body

Including binary data directly...

```python
>>> headers = {'Content-Type': 'application/json'}
>>> content = json.dumps(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=content)
```

## Working with content types

Including JSON request content...

```python
>>> data = httpx.JSON(...)
>>> httpx.Request('POST', 'https://echo.encode.io/', content=data)
```

Including form encoded request content...

```python
>>> data = httpx.Form(...)
>>> httpx.Request('PUT', 'https://echo.encode.io/', content=data)
```

Including multipart file uploads...

```python
>>> form = httpx.MultiPart(form={...}, files={...})
>>> with Request('POST', 'https://echo.encode.io/', content=form) as req:
>>> req.headers
{...}
>>> req.stream
<MultiPartStream [0% of ...MB]>
```

Including direct file uploads...

```python
>>> file = httpx.File('upload.json')
>>> with Request('POST', 'https://echo.encode.io/', content=file) as req:
>>> req.headers
{...}
>>> req.stream
<FileStream [0% of ...MB]>
```

## Accessing request content

...

---
```python
>>> data = request.json()
```

## Request
...

`<Request [GET “https://www.example.com”]>`
```python
>>> form = request.form()
```

...

*TODO*
```python
>>> files = request.files()
```

---

Expand Down
71 changes: 67 additions & 4 deletions docs/responses.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
# Responses

The core elements of an HTTP response are the `status_code`, `headers` and `body`.

```python
>>> resp = httpx.Response(200, headers={'Content-Type': 'text/plain'}, content=b'hello, world')
>>> resp
<Response [200 OK]>
>>> resp.status_code
200
>>> resp.headers
<Headers {'Content-Type': 'text/html'}>
>>> resp.body
b'hello, world'
```

## Working with the response headers

The following headers have automatic behavior with `Response` instances...

* `Content-Length` - Responses including a response body must always include either a `Content-Length` header or a `Transfer-Encoding: chunked` header. This header is automatically populated if `content` is not `None` and the content is a known size.
* `Transfer-Encoding` - Responses automatically include a `Transfer-Encoding: chunked` header if `content` is not `None` and the content is an unkwown size.
* `Content-Type` - Responses automatically include a `Content-Type` header if `content` is set using the [Content Type] API.

## Working with content types

Including HTML content...

```python
>>> content = httpx.HTML('<html><head>...</head><body>...</body></html>')
>>> response = httpx.Response(200, content=content)
```

Including plain text content...

```python
>>> content = httpx.Text('hello, world')
>>> response = httpx.Response(200, content=content)
```

Including JSON data...

```python
>>> content = httpx.JSON({'message': 'hello, world'})
>>> response = httpx.Response(200, content=content)
```

Including content from a file...

```python
>>> content = httpx.File('index.html')
>>> response = httpx.Response(200, content=content)
```

## Accessing response content

...

---
```
>>> content = response.body
```

## Response
...

`<Response [200 OK]>`
```
>>> text = response.text()
...
```

...

*TODO*
```
>>> data = response.json()
```

---

Expand Down