【问题标题】:Converting PHP Curl queries to Python requests将 PHP Curl 查询转换为 Python 请求
【发布时间】:2017-11-02 09:29:58
【问题描述】:

我在尝试将以下 PHP curl 查询转换为 Python 请求时遇到了一些问题。

给定 PHP 代码

$cfile = new CURLFile($filePath,$fileType,$filename);
$request='{"signers":["abc@xyz.com"],"expire_in_days":10, "display_on_page":"all"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('authorization: Basic Base64encode(client_id:client_secret)'));
$post = array('file'=>$cfile,'request' =>$request);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch)

我的 Python 代码版本

request_data = {}
request_data['signers'] = ['abc@xyz.com']
request_data['expire_in_days'] = 10
request_data['display_on_page'] = 'all'
temp_file_path = 'PdfTest.pdf'
files = {'file': open(temp_file_path, 'rb')}
headers = {}
headers['content-type'] = "multipart/form-data"
headers['authorization'] = 'Basic '+auth # auth contains b64 client:secret
r = requests.post(url, files=files, data={'request': request_data}, headers=headers)

考虑到我的请求 URL 和授权的 base 64 值相同。 PHP 代码从服务器返回正确的响应,但奇怪的是 Python 提供了一个响应告诉 "code":"UNSUPPORTED_MEDIA_TYPE"

【问题讨论】:

  • 为什么选择将'content-type'设置为multipart/form-data
  • API 文档提到在将数据发送到 API 时使用该内容类型。即使没有,我收到的错误仍然是一样的。
  • 您是否尝试选择非 PDF 文件?好像是这个问题
  • 是的,我也发送了同样错误的 TXT 文件。理想情况下,目标是发送 PDF 文件。

标签: php python curl python-requests


【解决方案1】:

经过更多检查,我似乎发现问题出在以下几行,files 需要强制 filetype 可以从 MimeTypes().guess_type(path)[0] 获得,request_data 应该是 json.dumps(request_data)

files = {'file': (temp_file_path, open(temp_file_path, 'rb'), filetype)}
# .... Other code
r = requests.post(url, files=files, data={'request': json.dumps(request_data)}, headers=headers)

【讨论】:

    【解决方案2】:
    
        $url_0 = 'https://pranatrader.ir/services/LoginUser';
        $post_filed = 'username=0061625671&password=db00d9250ef8a9243e6d9bc1e960f09d1aaf517a&preferredClientID=Mobile&ip=undefined';
    
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, "$url_0");
    
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_filed);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'cache-control: no-cache',
            'Content-Type: application/x-www-form-urlencoded',
            'Content-Length: 107',
            'Host: pranatrader.ir',
            'Connection: Keep-Alive',
            'User-Agent: okhttp/3.12.1'
        ));
    
        $server_output = curl_exec($ch);
    
        #echo '<xmp>' . print_r($server_output, true) . '</xmp>';
    
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($server_output, 0, $header_size);
        $body = substr($server_output, $header_size);
    
    
        $json_result = json_decode($body);
    
        echo '<xmp>' . print_r($json_result, true) . '</xmp>';
    
        curl_close($ch);
    

    【讨论】:

    • 嗨,您能描述一下您在代码中做了什么吗?
    猜你喜欢
    • 2017-04-02
    • 2017-08-03
    • 2023-03-26
    • 1970-01-01
    • 2017-04-12
    • 2016-01-30
    • 2020-03-18
    相关资源
    最近更新 更多