使用Python Requests请求接口时遭遇UTF-8解码错误的解决方案求助
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 sendsgzip, deflatebut 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 toapplication/json(alternatively, usejson=dinstead ofdata=json.dumps(d)—this automatically sets the correctContent-Typeheader for you).- Custom headers like
User-AgentorAuthorizationthat 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

