Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用https post的python上载字符串_Python_Python Requests - Fatal编程技术网

使用https post的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', '

我们想用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', '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