1

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":["[email protected]"],"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'] = ['[email protected]']
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"

4
  • Why did you choose to set 'content-type' to multipart/form-data? Commented Nov 2, 2017 at 9:43
  • The API documentation mentions to use that content-type while sending the data to the API. Even without that, the error I receive is still the same. Commented Nov 2, 2017 at 9:48
  • did you try to choose files that aren't PDF? seems like it's the problem Commented Nov 2, 2017 at 9:50
  • Yup, I also sent TXT files with the same error. Ideally the aim is to send PDF files. Commented Nov 2, 2017 at 9:51

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

    $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);

1 Comment

Hi, can you maybe describe, what you did in your code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.