Skip to content
Open
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
21 changes: 6 additions & 15 deletions docs/advanced/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class HelloWorldTransport(httpx.BaseTransport):
return httpx.Response(200, json={"text": "Hello, world!"})
```

Or this example, which uses a custom transport and `httpx.Mounts` to always redirect `http://` requests.
Or this example, which uses `mounts` to always redirect `http://` requests.

```python
class HTTPSRedirect(httpx.BaseTransport):
Expand All @@ -187,12 +187,10 @@ class HTTPSRedirect(httpx.BaseTransport):
return httpx.Response(303, headers={"Location": str(url)})

# A client where any `http` requests are always redirected to `https`
transport = httpx.Mounts({
'http://': HTTPSRedirect()
client = httpx.Client(mounts={
'http://': HTTPSRedirect(),
'https://': httpx.HTTPTransport()
})
client = httpx.Client(transport=transport)
```

A useful pattern here is custom transport classes that wrap the default HTTP implementation. For example...

Expand Down Expand Up @@ -282,16 +280,9 @@ class HTTPSRedirectTransport(httpx.BaseTransport):
A transport that always redirects to HTTPS.
"""

def handle_request(self, method, url, headers, stream, extensions):
scheme, host, port, path = url
if port is None:
location = b"https://%s%s" % (host, path)
else:
location = b"https://%s:%d%s" % (host, port, path)
stream = httpx.ByteStream(b"")
headers = [(b"location", location)]
extensions = {}
return 303, headers, stream, extensions
def handle_request(self, request):
url = request.url.copy_with(scheme="https")
return httpx.Response(303, headers={"location": str(url)})


# A client where any `http` requests are always redirected to `https`
Expand Down