2

I have the following Python code that makes a POST request to Clarifai's demographics endpoint:

import requests
import pprint

headers = {
    "Authorization": "Key MY_KEY",
    "Content-Type": "application/json"
}

data = {"inputs": [{"data": {"image": {"url": "https://samples.clarifai.com/demographics.jpg"}}}]}

proxies = {
    "http": "MY_HTTP_PROXY", 
    "https": "MY_HTTPS_PROXY"
}

response = requests.post('https://api.clarifai.com/v2/models/c0c0ac362b03416da06ab3fa36fb58e3/outputs', headers=headers, data=data, proxies=proxies, verify=False)

pprint.pprint(response.json())

Note that I've replaced my real api key and proxies with MY_KEY, MY_HTTP_PROXY, and MY_HTTPS_PROXY respectively.

Does anyone experienced with Clarifai know what I'm doing wrong? I saw an example of working code posted on Clarifai's own forum, but I can't see any major differences between the working code and mine.

ElsaInSpirit
  • 341
  • 6
  • 16

2 Answers2

1

Just convert the data passed to json.

import requests
import pprint
import json

headers = {
    "Authorization": "Key MY_KEY",
    "Content-Type": "application/json"
}

data = {"inputs": [{"data": {"image": {"url": "https://samples.clarifai.com/demographics.jpg"}}}]}

json_data = json.dumps(data)

proxies = {
    "http": "MY_HTTP_PROXY", 
    "https": "MY_HTTPS_PROXY"
}

response = requests.post('https://api.clarifai.com/v2/models/c0c0ac362b03416da06ab3fa36fb58e3/outputs', headers=headers, data=json_data, proxies=proxies, verify=False)

pprint.pprint(response.json())
NoName
  • 11
  • 1
-1

Needed quotes around the data variable

'data = {"inputs": [{"data": {"image": {"url": "https://samples.clarifai.com/demographics.jpg"}}}]}'
ElsaInSpirit
  • 341
  • 6
  • 16