使用请求将字符串发布为multipart/form-data [英] Post a string as multipart/form-data using requests

查看:133
本文介绍了使用请求将字符串发布为multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发布一个API,该API似乎坚持以名称(文件名?)xml的形式接收XML数据作为multipart/form-data.它可以在邮递员中工作,但是我无法使用Python的请求来工作.这是我的Python代码(基于 https://stackoverflow.com/a/24443309/1011724 ):

I am posting to an API that seems to insist on receiving XML data as multipart/form-data with the name (file name?) xml. It works in postman but I can't get it to work using Python's requests. This is my Python code (based on https://stackoverflow.com/a/24443309/1011724):

requests.post(callpro_url,
              files={'xml':('data.xml',result)},
              verify=False).text

其中,result是包含XML的字符串.如果我尝试此代码,则会得到响应:

where result is a string containing XML. If I try this code I get the response:

xml帖子字段为空

xml post field is empty

如果您不使用multipart/form-data标头,则此API给出的响应.

which is the response this API give if you don't use the multipart/form-data header.

如果我从工作中的邮递员职位生成代码,我将得到类似(略述)的内容:

If I generate code from the working postman post I get something like this (slightly redacted):

import requests

url = "https://blablabla.blablab.com/blabla/api.php"

querystring = {"mode":"import","hash":"redacted-hash","xml":"\"xml\""}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"xml\"\r\n\r\n<?xml version=\"1.0\" ?>\n<importdata>\n --redacted-XML-- \n</importdata>\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'postman-token': "8d3ec8ee-784e-3a65-5240-cf1a9534d1c4"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

在Python中执行此代码,它会给出正确的响应.

Executing this code in Python, it gives the correct response.

请注意,邮递员代码中的params=querystring部分是在我的代码中的URL中处理的.

Note that the params=querystring part in the postman code is taken care of in the URL in my code.

我对邮递员代码中的有效载荷感到困惑.它在字符串中添加诸如Content-Disposition和name之类的内容.我假设我可以将这些东西放在files参数的元组中,但是我不确定该怎么做.例如,我已经尝试过files={'xml':('data.xml',result,'form-data')}以及files={'xml':('data.csv',result)}{'xml':('xml',result)}.

I'm confused by the payload in the postman code. It adds things like Content-Disposition and name in the string. I assume that I can put this stuff in the tuple in the files parameter but I'm not sure how to to do it. I've tried files={'xml':('data.xml',result,'form-data')} for example and also files={'xml':('data.csv',result)} and {'xml':('xml',result)}.

此外,邮递员代码将标头明确定义为

Also, the postman code explicitly defines the header as

 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"

请求文档说,我永远不要以这种方式显式定义标题.但是邮递员代码有效,而我的代码无效.

whereas the requests documentation say that I should never explicitly define the headers in that way. However the postman code works and my code does not.

有什么建议吗?

推荐答案

我不知道这是否值得回答,还是应该删除问题,但需要的是将文件命名为"xml"而不是xml .这样就可以了:

I don't know if this is worth answering or if I should delete the question but what was needed was to name the file "xml" not xml. so this works:

requests.post(callpro_url,
              files={'xml':('"xml"',result)},
              verify=False).text

这篇关于使用请求将字符串发布为multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆