It looks like the issue you’re facing with the Content-Type header showing up as b'text/html; charset=utf-8' is because of the response being interpreted as a byte string.
The b prefix indicates that the string is in bytes format rather than a regular string. This usually happens when there is a mismatch in the encoding or the way the headers are set in your FastAPI response.
To fix this issue, you need to ensure that the Content-Type header is explicitly set and correctly encoded.
Hi Pawlean! I’m encountering the same issue here and was not able to fix it. I’m a bit puzzled because it works fine on my local machine and only has this issue on Vercel. I added some middleware, but no dice.
Example header…
content-type: b’application/json; charset=utf-8’
Would this imply it’s an encoding or decoding issue? My site is https://howdy.new if you’d like to see this in action.
# Middleware to ensure all headers are strings @app.middleware(“http”)
async def ensure_string_headers_middleware(request: Request, call_next):
response = await call_next(request)
if isinstance(response, StreamingResponse):
# For streaming responses, we need to modify the headers in a different way
original_headers = response.headers
response.raw_headers = [(k.encode('utf-8'), v.encode('utf-8'))
for k, v in ensure_string_headers(original_headers).items()]
elif isinstance(response, Response):
# For regular responses, we can modify the headers directly
response.headers = ensure_string_headers(response.headers)
return response