在 Python 2.7.6 中使用 Requests 2.2.1 发送 .csv 文件
Send a .csv file using Requests 2.2.1 in Python 2.7.6
我必须使用他们提供的 API 将一个小的 .csv 文件发送到合作伙伴服务器。我在 Python 2.7.6 中使用 Requests 2.2.1,并且在 PHP 中有一个工作示例。我不能提供更多信息,但我想知道是否有人可以根据我的错误代码(在 Python 中)的输出和工作代码的输出(在 PHP 中)看到问题是什么)
这是我的精简版,基于请求发送文件 (POST a Multipart-Encoded File)
的文档
import os
import requests
filecontent=open(filename,'r').read()
r=requests.post( url
, data={'sessionId':sessionId, 'source':'Neurotracker'}
, files={'files': (os.path.basename(filename), filecontent, 'application/octet-stream')} )
print('response text=[{}]'.format(r.text))
这是我得到的那种输出
response text=[{"status":"error","error":{"descr":"Unknown error in FACTS file save path - sites\/default\/files\/facts\/csv\/2015\/04 _FILES is =
Array
(
[files] => Array
(
[name] => 344_2015-04-08T145040.csv
[type] => application\/octet-stream
[tmp_name] => \/tmp\/phpW6jG0w
[error] => 0
[size] => 223
)
)","number":106}}]
这是 PHP
中的一个工作示例
$post = array(
'sessionId' => $sessionId,
'source' => 'Polar',
'files[factfile]' => '@' . $file_name_with_full_path
);
$ch = curl_init();
// May want to have error checking code for the init.
//
// note order is important. POST opt sett must come before POSTFIELDs, according to docs...
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// You should have some error handling for the result. One option is to print_r($result,true); dpm($result);
curl_close($ch);
这将是一个好的文件上传的输出
Array
(
[files] =\x3e Array
(
[name] =\x3e Array
(
[factfile] =\x3e 344_2015-04-08T145040.csv
)
[type] =\x3e Array
(
[factfile] =\x3e application/octet-stream
)
[tmp_name] =\x3e Array
(
[factfile] =\x3e /Applications/MAMP/tmp/php/phpQfSVVK
)
[error] =\x3e Array
(
[factfile] =\x3e 0
)
[size] =\x3e Array
(
[factfile] =\x3e 223
)
)
)
我注意到 [files] 的所有子键都是一个好的文件上传输出中的数组,而我的不是。所以我尝试了使用 Requests (POST Multiple Multipart-Encoded Files)
发送多个文件的方法
r=requests.post( url
, data=payload
, files=[('files', (os.path.basename(filename), filecontent, 'application/octet-stream'))] );
没有更多的运气。
我感觉Requests可能无法通过API实现预期格式的文件上传;那将与 [files] 的子键中的数组一起使用。
这对我来说有点黑魔法,但只需要将 'files'
更改为 'files[factfile]'
:
r=requests.post( url
, data={'sessionId':sessionId, 'source':'Neurotracker'}
, files={'files[factfile]': (os.path.basename(filename), filecontent, 'application/octet-stream')} )
欢迎任何想解释为什么这样做的人
我必须使用他们提供的 API 将一个小的 .csv 文件发送到合作伙伴服务器。我在 Python 2.7.6 中使用 Requests 2.2.1,并且在 PHP 中有一个工作示例。我不能提供更多信息,但我想知道是否有人可以根据我的错误代码(在 Python 中)的输出和工作代码的输出(在 PHP 中)看到问题是什么)
这是我的精简版,基于请求发送文件 (POST a Multipart-Encoded File)
的文档import os
import requests
filecontent=open(filename,'r').read()
r=requests.post( url
, data={'sessionId':sessionId, 'source':'Neurotracker'}
, files={'files': (os.path.basename(filename), filecontent, 'application/octet-stream')} )
print('response text=[{}]'.format(r.text))
这是我得到的那种输出
response text=[{"status":"error","error":{"descr":"Unknown error in FACTS file save path - sites\/default\/files\/facts\/csv\/2015\/04 _FILES is =
Array
(
[files] => Array
(
[name] => 344_2015-04-08T145040.csv
[type] => application\/octet-stream
[tmp_name] => \/tmp\/phpW6jG0w
[error] => 0
[size] => 223
)
)","number":106}}]
这是 PHP
中的一个工作示例$post = array(
'sessionId' => $sessionId,
'source' => 'Polar',
'files[factfile]' => '@' . $file_name_with_full_path
);
$ch = curl_init();
// May want to have error checking code for the init.
//
// note order is important. POST opt sett must come before POSTFIELDs, according to docs...
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// You should have some error handling for the result. One option is to print_r($result,true); dpm($result);
curl_close($ch);
这将是一个好的文件上传的输出
Array
(
[files] =\x3e Array
(
[name] =\x3e Array
(
[factfile] =\x3e 344_2015-04-08T145040.csv
)
[type] =\x3e Array
(
[factfile] =\x3e application/octet-stream
)
[tmp_name] =\x3e Array
(
[factfile] =\x3e /Applications/MAMP/tmp/php/phpQfSVVK
)
[error] =\x3e Array
(
[factfile] =\x3e 0
)
[size] =\x3e Array
(
[factfile] =\x3e 223
)
)
)
我注意到 [files] 的所有子键都是一个好的文件上传输出中的数组,而我的不是。所以我尝试了使用 Requests (POST Multiple Multipart-Encoded Files)
发送多个文件的方法r=requests.post( url
, data=payload
, files=[('files', (os.path.basename(filename), filecontent, 'application/octet-stream'))] );
没有更多的运气。
我感觉Requests可能无法通过API实现预期格式的文件上传;那将与 [files] 的子键中的数组一起使用。
这对我来说有点黑魔法,但只需要将 'files'
更改为 'files[factfile]'
:
r=requests.post( url
, data={'sessionId':sessionId, 'source':'Neurotracker'}
, files={'files[factfile]': (os.path.basename(filename), filecontent, 'application/octet-stream')} )
欢迎任何想解释为什么这样做的人