使用https post的python上载字符串
我们想用python上传一个字符串。 我找到了一些示例并创建了以下脚本使用https post的python上载字符串,python,python-requests,Python,Python Requests,我们想用python上传一个字符串。 我找到了一些示例并创建了以下脚本 import requests url = 'https://URL/fileupload?FileName=file.csv' headers = {'content-type': 'octet-stream'} files = {'file': ('ID,Name\n1,test')} r = requests.post(url, files=files, headers=headers, auth=('user', '
import requests
url = 'https://URL/fileupload?FileName=file.csv'
headers = {'content-type': 'octet-stream'}
files = {'file': ('ID,Name\n1,test')}
r = requests.post(url, files=files, headers=headers, auth=('user', 'password'))
上载正在工作,但输出包含一些意外的行
--ec7b507f800f48ab85b7b36ef40cfc44
Content-Disposition: form-data; name="file"; filename="file"
ID,Name
1,test
--ec7b507f800f48ab85b7b36ef40cfc44--
目标是仅从文件={'file':('ID,Name\n1,test')}
上载以下内容:
这是怎么可能的?当使用
文件参数时,请求创建发布文件所需的标题和正文。
如果您不希望将请求格式化为那样的格式,可以使用data
参数
url = 'http://httpbin.org/anything'
headers = {'content-type': 'application/octet-stream'}
files = {'file': 'ID,Name\n1,test'}
r = requests.post(url, data=files, headers=headers, auth=('user', 'password'))
print(r.request.body)
请注意,当将字典传递给数据时,它会得到url编码。如果要提交数据而不使用任何编码,可以使用字符串
url = 'http://httpbin.org/anything'
headers = {'content-type': 'application/octet-stream'}
files = 'ID,Name\n1,test'
r = requests.post(url, data=files, headers=headers, auth=('user', 'password'))
print(r.request.body)
file=ID%2CName%0A1%2Ctest
url = 'http://httpbin.org/anything'
headers = {'content-type': 'application/octet-stream'}
files = 'ID,Name\n1,test'
r = requests.post(url, data=files, headers=headers, auth=('user', 'password'))
print(r.request.body)
ID,Name
1,test