将PHP Curl查询转换为Python请求
I've been facing a few problems trying to convert the following PHP curl queries to Python requests.
Given PHP Code
$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)
My version of Python code
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)
Considering my request URLs are the same and so is the base 64 value for authorization. The PHP code returns the right response from the server but the Python one strangely says provides a response telling "code":"UNSUPPORTED_MEDIA_TYPE"
我在尝试将以下PHP curl查询转换为Python请求时遇到了一些问题。 p>
给定PHP代码 h3>
$ 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)
code> pre>
我的Python代码版本 h3>
request_data = {}
request_data ['signers'] = ['abc@xyz.com'] \ nrequest_data ['expire_in_days'] = 10
request_data ['display_on_page'] ='all'
nmp_file_path ='PdfTest.pdf'
files = {'file':open(temp_file_path,'rb')}
header s = {}
headers ['content-type'] =“multipart / form-data”
headers ['authorization'] ='基本'+ auth #auth包含b64客户端:secret
r = requests.post(url, files = files,data = {'request':request_data},headers = headers)
code> pre>
考虑我的请求URL是相同的,因此base 64的值是 授权。 PHP代码从服务器返回正确的响应,但Python奇怪地说提供了一个响应告诉“code”:“UNSUPPORTED_MEDIA_TYPE” code> p>
div>
$ 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)
code> pre>
我的Python代码版本 h3>
request_data = {}
request_data ['signers'] = ['abc@xyz.com'] \ nrequest_data ['expire_in_days'] = 10
request_data ['display_on_page'] ='all'
nmp_file_path ='PdfTest.pdf'
files = {'file':open(temp_file_path,'rb')}
header s = {}
headers ['content-type'] =“multipart / form-data”
headers ['authorization'] ='基本'+ auth #auth包含b64客户端:secret
r = requests.post(url, files = files,data = {'request':request_data},headers = headers)
code> pre>
考虑我的请求URL是相同的,因此base 64的值是 授权。 PHP代码从服务器返回正确的响应,但Python奇怪地说提供了一个响应告诉“code”:“UNSUPPORTED_MEDIA_TYPE” code> p>
div>
After some more checks, I seemed to figure out the issue was with the following lines, files
needs mandatory filetype
which can be obtained from MimeTypes().guess_type(path)[0]
and the request_data
should have been 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)