Curl to Request

HI all
I’m struggling to convert the following Curl to Requests.

curl -X POST \
  -H "Authorization: Key API_key" \
  -H "Content-Type: application/json" \
  -d '
  {
    "inputs": [
      {
        "data": {
          "image": {
            "base64": "'"$(base64 /Users/robincole/Documents/Github/simple-clarifai/metro-north.jpg)"'"
          }
        }
      }
    ]
  }'\
  https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs

I’ve tried several variants on the following with no success:

HEADERS = {'content-type': 'application/json', 'Authorization': 'Key ' +API_key}

with open(filename, 'rb') as image_data:
    response = requests.get(url=ENDPOINT_URL, headers=HEADERS, data=image_data)

Cheers

Building on Paulus’ answer in discord, here’s some code that should work and does the base64 encoding:

import requests
import base64
import pathlib

img_file_data = pathlib.Path("/Users/robincole/Documents/Github/simple-clarifai/metro-north.jpg").read_bytes()
base64_img = base64.b64encode(img_file_data).decode('ascii')
json_data = {"inputs": [{"data": {"image": {"base64": base64_img}}}]}
headers = {
    'Authorization': "Key API_KEY",
    'Content-Type': 'application/json',
}
requests.post("https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs", 
              headers=headers, json=json_data)
1 Like