Httpx Generating Incorrect Content-Length Header Value Using Json Param #3598
-
|
Working with some 3rd party apis. I noticed that I was failing a request to a 3rd party api which is passing in Postman and via Pythons requests package. The httpx is on latest from pypi 0.28.1 import requests
import httpx
import aiohttp
import asyncio
import json
body = {
"identifiers":
[
"number",
"name"
],
"page": 1,
"per_page": 100
}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'x': 'xxxx'
}
url = "https://httpbin.org/post"
httpx_response = httpx.post(url, headers=headers, json=body)
requests_response = requests.post(url, headers=headers, json=body)
aiohttp_content_length = None
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=body) as aiohttp_response:
global aiohttp_content_length
aiohttp_content_length = aiohttp_response._request_info.headers.get('Content-Length', 0)
asyncio.run(fetch())
print("################ Content-Length #################")
print(f"Script calculated: {len(json.dumps(body).encode('utf-8'))}")
print(f"Httpx calculated: {httpx_response.request.headers.get('Content-Length', 0)}")
print(f"Requests calculated: {requests_response.request.headers.get('Content-Length', 0)}")
print(f"aiohttp calculated: {aiohttp_content_length}")
print("#################################################\n")Here is the output: ################ Content-Length #################
Script calculated: 63
Httpx calculated: 57
Requests calculated: 63
aiohttp calculated: 63
#################################################I checked the issues and discussions for There could be something I am doing wrong and please let me know and I will adjust the code and test again. Environments I've tried and python versions |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Verified this is working correctly in version |
Beta Was this translation helpful? Give feedback.
-
|
Line 38 in 4fb9528 You're not seeing an incorrect Content-Length. You're seeing a more compact representation being used. |
Beta Was this translation helpful? Give feedback.
httpx/CHANGELOG.md
Line 38 in 4fb9528
You're not seeing an incorrect Content-Length. You're seeing a more compact representation being used.