简体   繁体   English

请求:发布多部分/表单数据

[英]requests: post multipart/form-data

My API:我的API:

class FileView(APIView):
    parser_classes = (MultiPartParser,)
    def post(self, request):
        do something with request.FILES.dict().iteritems()

My requests file:我的请求文件:

try:
    headers = {
        'content-type': "multipart/form-data",
        'authorization': "Basic ZXNlbnRpcmVcYdddddddddddddd",
        'cache-control': "no-cache",
    }
    myfile = {"file": ("filexxx", open(filepath, "rb"))}

    response = requests.request("POST", verify=False, url=url, data=myfile, headers=headers)

    print(response.text)
except Exception as e:
    print "Exception:", e

Error:错误:

"Multipart form parse error - Invalid boundary in multipart: None" “多部分表单解析错误 - 多部分中的无效边界:无”

What is right way to post the file?发布文件的正确方法是什么? Thanks谢谢

requests.请求。 version '2.10.0'版本“2.10.0”

Removed 'content-type' from the headers, now it works从标题中删除了“内容类型”,现在可以使用了

try:
    headers = {
        'authorization': "Basic ZXNlbnRpcmVcYdddddddddddddd",
        'cache-control': "no-cache",
    }
    myfile = {"file": ("filexxx", open(filepath, "rb"))}

    response = requests.request("POST", verify=False, url=url, data=myfile, headers=headers)

    print(response.text)
except Exception as e:
    print "Exception:", e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM