You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python Requests请求接口时遭遇UTF-8解码错误的解决方案求助

Troubleshooting UnicodeDecodeError When Parsing Requests Response

Let’s work through this issue step by step—since Postman works perfectly, we know the server is returning valid content, so the problem lies in how requests is handling or decoding the response.

1. Let requests Handle Decoding Automatically

Instead of manually decoding r.content, try using r.text directly. The requests library automatically infers the correct encoding from both response headers and raw content, which might fix the mismatch:

r = requests.post("Anonymous site", data=json.dumps(d), headers=h2)
print(r.text)
print(r.encoding)
print(r.apparent_encoding)  # This shows the encoding inferred from actual content, not just headers

If r.apparent_encoding is different from r.encoding, use it to decode explicitly:

print(r.content.decode(r.apparent_encoding))

2. Verify Your Request Headers Match Postman Exactly

Postman works, so cross-check every header in your h2 dictionary against what Postman sends. Common mismatches that cause this issue include:

  • Accept-Encoding: If Postman sends gzip, deflate but your code doesn’t, the server might return compressed content you’re trying to decode as plain text.
  • Content-Type: Since you’re sending JSON, ensure this is set to application/json (alternatively, use json=d instead of data=json.dumps(d)—this automatically sets the correct Content-Type header for you).
  • Custom headers like User-Agent or Authorization that the server expects to validate the request.

Pro tip: Use Postman’s "Code" feature (found under the "Send" button) to generate a Python requests snippet. Compare this to your code to spot missing or mismatched headers instantly.

3. Check if the Response is Compressed

If the server returns compressed content (gzip/deflate) and requests isn’t auto-decompressing it, you’ll get unreadable bytes that throw decode errors. You can either force auto-decompression via headers, or manually decompress:

import gzip
from io import BytesIO

r = requests.post("Anonymous site", data=json.dumps(d), headers=h2)
if r.headers.get('Content-Encoding') == 'gzip':
    compressed_data = BytesIO(r.content)
    decompressed_data = gzip.GzipFile(fileobj=compressed_data)
    print(decompressed_data.read().decode('utf-8'))

4. Inspect the Raw Response Content

If none of the above works, save the raw response to a file to see exactly what you’re receiving—this can reveal if it’s binary data, a non-text response, or a completely different encoding:

with open('response_raw.bin', 'wb') as f:
    f.write(r.content)

Open this file in a hex editor or Notepad++ (with encoding detection enabled) to compare it against what Postman shows in its response panel.


内容的提问来源于stack exchange,提问作者kokiwebaaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 10:27:29