Skip to content
Myr

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

2 files+23 added−6 removed

modified:httpcore/models.py+8 added-3 removed
42 unmodified lines
43 typing.Dict[typing.AnyStr, typing.AnyStr],44 typing.List[typing.Tuple[typing.AnyStr, typing.AnyStr]],45]4647RequestData = typing.Union[bytes, typing.AsyncIterator[bytes]]47RequestData = typing.Union[dict, bytes, typing.AsyncIterator[bytes]]4849ResponseContent = typing.Union[bytes, typing.AsyncIterator[bytes]]5051
146 unmodified lines
198 def __hash__(self) -> int:199 return hash((self.is_ssl, self.host, self.port))200201202class QueryParams(typing.Mapping):202class QueryParams(typing.Mapping[str, str]):203 """204 URL query parameters, as a multi-dict.205 """206
256 unmodified lines
463 headers: HeaderTypes = None,464 ):465 self.method = method.upper()466 self.url = URL(url, query_params=query_params)467 self.headers = Headers(headers)468469 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)479480 async def read(self) -> bytes:481 """482 Read and return the response content.
271 unmodified lines
modified:tests/models/test_requests.py+15 added-3 removed
26 unmodified lines
27 ]28 )293031def 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"434445def test_transfer_encoding_header():46 async def streaming_body(data):47 yield data # pragma: nocover48
36 unmodified lines
8586 data = streaming_body(b"test 123")87 headers = [(b"content-length", b"8")]8875 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