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

requests是否支持multipart响应?接收该响应时遇解析报错求助

Does requests fully support multipart responses? Troubleshooting your parsing error

Yes, the requests library does support multipart responses (it leverages urllib3's parsing capabilities under the hood), but the error you're seeing points to a problem with the server's response formatting—not a lack of support in requests itself.

Let's break down your error

The warnings StartBoundaryNotFoundDefect() and MultipartInvariantViolationDefect() tell us two key issues:

  • The server’s response headers claim the content is multipart (via the Content-Type header), but urllib3 can’t locate the required boundary string that separates parts of the response.
  • Or, the response body doesn’t follow the HTTP specification for multipart content (e.g., empty body, missing closing boundary, or malformed part structure).

Common fixes and debugging steps

Here are actionable steps to resolve this:

  1. Validate the Content-Type header
    Multipart responses must include a boundary parameter in the Content-Type header. For example:

    Content-Type: multipart/form-data; boundary=----MyUniqueBoundary123
    

    If the boundary is missing, malformed, or wrapped in invalid quotes, urllib3 will fail to parse the response. Check this by printing response.headers['Content-Type'] after making your request.

  2. Inspect the raw response body
    Servers sometimes send an empty body even when declaring a multipart content type. Use response.content to view the raw bytes returned. If it’s empty or doesn’t start with --{boundary} (the expected multipart delimiter), that’s the root cause.

  3. Manual parsing workaround
    If the server’s response is malformed but still contains valid multipart content, bypass urllib3’s strict parsing with Python’s built-in email module:

    import requests
    from email.parser import BytesParser
    from email.policy import default
    
    url = "your-target-url"
    response = requests.get(url)
    
    # Extract boundary from Content-Type header
    content_type = response.headers.get("Content-Type", "")
    boundary = None
    for param in content_type.split(";"):
        param = param.strip()
        if param.startswith("boundary="):
            boundary = param.split("=", 1)[1].strip('"')
    
    if boundary:
        # Parse the multipart content
        msg = BytesParser(policy=default).parsebytes(response.content)
        for part in msg.walk():
            if part.is_multipart():
                continue  # Skip nested multipart sections
            print(f"Part Content-Type: {part.get_content_type()}")
            print(f"Part Payload (decoded): {part.get_payload(decode=True)}")
    else:
        print("No valid boundary found in Content-Type header")
    
  4. Rule out proxy/intermediary issues
    Proxies or middle servers sometimes modify response headers or bodies, breaking parsing. Try making the request directly to the server (if possible) to eliminate this variable.

Final note

Requests relies on urllib3’s strict adherence to HTTP standards for parsing multipart responses. Most often, this error comes from a non-compliant server response rather than a limitation in requests.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:23:35