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

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

1 Answers1

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

Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25