Replies: 1 comment
-
|
Okay, it's a little bit ambiguous what behavior you'd expect in those cases. I don't think this is related to #2382, it looks like the same behaviour that we've had for a while... import httpx
# Client with Content-Type headers set.
client = httpx.Client(headers={"Content-Type": "application/json"})
# An `application/x-www-form-urlencoded` POST request.
# Essentially the same case as your multipart example, but just a little more simple.
request = client.build_request("POST", "https://www.example.com", data={"a": "b"})
print(request.headers)
print(request.content)Across a few different version of # 0.20.0
Headers({'host': 'www.example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.20.0', 'content-type': 'application/json', 'content-length': '3'})
b'a=b'
# 0.21.0
Headers({'host': 'www.example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.21.0', 'content-type': 'application/json', 'content-length': '3'})
b'a=b'
# 0.22.0
Headers({'host': 'www.example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.22.0', 'content-type': 'application/json', 'content-length': '3'})
# latest
Headers({'host': 'www.example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.23.2', 'content-type': 'application/json', 'content-length': '3'})
b'a=b'All the cases have The quickest solution to the issue would be to stop doing this... # Not needed. You'll get this header if you use `json=...` anyways.
# Specifying a `Content-Type` header on the client instance is almost always going to
# be a bad idea. Eg. you'll also get odd looking GET requests which have a `Content-Type`
# header but no content.
client = httpx.Client(headers={"Content-Type": "application/json"})But, yeah, there's other options too. I can see that we might be able to change the priority rules so that client headers only get applied if they're not overriding the auto-generated request headers. Or... we could guard against setting certain headers on the client. In particular we could prevent setting the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
After testing #2382 it seems that the request headers are not being updated correctly.
The request has the following header
{'Content-Type': 'multipart/form-data; boundary=b194f2c9bc744ebf40c3fa03d6d53987'}However if using an initialized client:
The request is still using
{'Content-Type': 'application/json'}Error message from our API:
'{"code":"API_MALFORMED_BODY","message":"Malformed JSON"}'Shouldn't the POST change the headers if using multipart forms?
Specifying the following also does not work because the boundaries are not included in the headers:
resp = client.post('URL', files=data, headers={'Content-Type': 'multipart/form-data'})Beta Was this translation helpful? Give feedback.
All reactions