繁体   English   中英

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

[英]requests: post multipart/form-data

我的API:

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

我的请求文件:

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

错误:

“多部分表单解析错误 - 多部分中的无效边界:无”

发布文件的正确方法是什么? 谢谢

请求。 版本“2.10.0”

从标题中删除了“内容类型”,现在可以使用了

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