0

I have an existing API that I connect to using PHP and send an image, the code looks like this...

$url = 'https://example.com/api';
$ch = \curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Subscription-Key: 345sdf4'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
            \"fileurl\":\"" . $this->_config['server_config']['server_url'] . $my_image . "\"
        }");

I am trying to convert this to Python3, I understand I need requests so have this so far...

import requests

api_url_base = 'https://example.com/api'
headers = {'Content-Type': 'application/json',
       'Subscription-Key': '345sdf4'}

But this is as far as I have got, how do I add the image? I know the absolute path of the image already

1
  • Your POST body would be something like json.dumps(dict({'fileurl': myUrl})) Commented Aug 8, 2019 at 23:17

1 Answer 1

2

According to this post you can do the following:

import requests
import json

url = 'https://example.com/api'
body = {'name': 'something'}
headers = {'content-type': 'application/json'}

r = requests.post(url, data=json.dumps(body), headers=headers)

source

Sign up to request clarification or add additional context in comments.

Comments

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.