Support url-encoded data in requests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by Tom Christie · parent 19542f2 · Browse files · 2 files·+23 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+23 added−6 removed
- tests:+15 added−3 removed
- other:+8 added−3 removed
Provenance
- Commit
e91c69d- Tree
6a8c964- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
19542f2
2 files changed+23 added−6 removed
modified:httpcore/models.py+8 added-3 removed
42 unmodified lines43 typing.Dict[typing.AnyStr, typing.AnyStr],44 typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],45 ]46 47-RequestData = typing.Union[bytes, typing.AsyncIterator[bytes]]47+RequestData = typing.Union[dict, bytes, typing.AsyncIterator[bytes]]48 49 ResponseContent = typing.Union[bytes, typing.AsyncIterator[bytes]]50 51 146 unmodified lines198 def __hash__(self) -> int:199 return hash((self.is_ssl, self.host, self.port))200 201 202-class QueryParams(typing.Mapping):202+class QueryParams(typing.Mapping[str, str]):203 """204 URL query parameters, as a multi-dict.205 """206 256 unmodified lines463 headers: HeaderTypes = None,464 ):465 self.method = method.upper()466 self.url = URL(url, query_params=query_params)467+ self.headers = Headers(headers)468+469 if isinstance(data, bytes):470 self.is_streaming = False471 self.content = data472+ elif isinstance(data, dict):473+ self.is_streaming = False474+ self.content = urlencode(data, doseq=True).encode("utf-8")475+ self.headers["Content-Type"] = "application/x-www-form-urlencoded"476 else:477 self.is_streaming = True478 self.content_aiter = data473- self.headers = Headers(headers)479 480 async def read(self) -> bytes:481 """482 Read and return the response content.271 unmodified linesmodified:tests/models/test_requests.py+15 added-3 removed
26 unmodified lines27 ]28 )29 30 31+def test_url_encoded_data():32+ request = httpcore.Request("POST", "http://example.org", data={"test": "123"})33+ request.prepare()34+ assert request.headers == httpcore.Headers(35+ [36+ (b"host", b"example.org"),37+ (b"content-length", b"8"),38+ (b"accept-encoding", b"deflate, gzip, br"),39+ (b"content-type", b"application/x-www-form-urlencoded"),40+ ]41+ )42+ assert request.content == b"test=123"43+44+45 def test_transfer_encoding_header():46 async def streaming_body(data):47 yield data # pragma: nocover48 36 unmodified lines85 86 data = streaming_body(b"test 123")87 headers = [(b"content-length", b"8")]88 75- request = httpcore.Request(76- "POST", "http://example.org", data=data, headers=headers77- )89+ request = httpcore.Request("POST", "http://example.org", data=data, headers=headers)90 request.prepare()91 assert request.headers == httpcore.Headers(92 [93 (b"host", b"example.org"),27 unmodified lines