Support url-encoded data in requests
This commit is contained in:
parent
19542f2c3c
commit
e91c69d2ad
@ -44,7 +44,7 @@ HeaderTypes = typing.Union[
|
||||
typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],
|
||||
]
|
||||
|
||||
RequestData = typing.Union[bytes, typing.AsyncIterator[bytes]]
|
||||
RequestData = typing.Union[dict, bytes, typing.AsyncIterator[bytes]]
|
||||
|
||||
ResponseContent = typing.Union[bytes, typing.AsyncIterator[bytes]]
|
||||
|
||||
@ -199,7 +199,7 @@ class Origin:
|
||||
return hash((self.is_ssl, self.host, self.port))
|
||||
|
||||
|
||||
class QueryParams(typing.Mapping):
|
||||
class QueryParams(typing.Mapping[str, str]):
|
||||
"""
|
||||
URL query parameters, as a multi-dict.
|
||||
"""
|
||||
@ -464,13 +464,18 @@ class Request:
|
||||
):
|
||||
self.method = method.upper()
|
||||
self.url = URL(url, query_params=query_params)
|
||||
self.headers = Headers(headers)
|
||||
|
||||
if isinstance(data, bytes):
|
||||
self.is_streaming = False
|
||||
self.content = data
|
||||
elif isinstance(data, dict):
|
||||
self.is_streaming = False
|
||||
self.content = urlencode(data, doseq=True).encode("utf-8")
|
||||
self.headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
else:
|
||||
self.is_streaming = True
|
||||
self.content_aiter = data
|
||||
self.headers = Headers(headers)
|
||||
|
||||
async def read(self) -> bytes:
|
||||
"""
|
||||
|
||||
@ -28,6 +28,20 @@ def test_content_length_header():
|
||||
)
|
||||
|
||||
|
||||
def test_url_encoded_data():
|
||||
request = httpcore.Request("POST", "http://example.org", data={"test": "123"})
|
||||
request.prepare()
|
||||
assert request.headers == httpcore.Headers(
|
||||
[
|
||||
(b"host", b"example.org"),
|
||||
(b"content-length", b"8"),
|
||||
(b"accept-encoding", b"deflate, gzip, br"),
|
||||
(b"content-type", b"application/x-www-form-urlencoded"),
|
||||
]
|
||||
)
|
||||
assert request.content == b"test=123"
|
||||
|
||||
|
||||
def test_transfer_encoding_header():
|
||||
async def streaming_body(data):
|
||||
yield data # pragma: nocover
|
||||
@ -72,9 +86,7 @@ def test_override_content_length_header():
|
||||
data = streaming_body(b"test 123")
|
||||
headers = [(b"content-length", b"8")]
|
||||
|
||||
request = httpcore.Request(
|
||||
"POST", "http://example.org", data=data, headers=headers
|
||||
)
|
||||
request = httpcore.Request("POST", "http://example.org", data=data, headers=headers)
|
||||
request.prepare()
|
||||
assert request.headers == httpcore.Headers(
|
||||
[
|
||||
|
||||
Loading…
Reference in New Issue
Block a user