Sumit Kumar
Sumit Kumar

Reputation: 51

'Malformed or invalid request' with clarifai api

what is wrong with the following request for clarifai api-

import requests
image_url='https://samples.clarifai.com/food.jpg'
api='Key cb03ceba3c8842aeadd55dcb2f0be152'
headers = {
    'Authorization': api,
    'Content-Type': 'application/json',
}

data = '{"inputs": [{"data": {"image": {"url": image_url}}}]}'

url='https://api.clarifai.com/v2/models/bd367be194cf45149e75f01d59f77ba7/outputs'
response = requests.post(url=url, headers=headers, data=data)
print(response.status_code, response.json())

i keep hitting this error-

400 {'status': {'code': 11102, 'description': 'Invalid request', 'details': 'Malformed or invalid request'}}

Upvotes: 1

Views: 956

Answers (4)

Andrew Mugisha
Andrew Mugisha

Reputation: 11

I had the same problem; I solved it by converting the variable holding the image URL to a string. In your case, just make the following change:

data = '{"inputs": [{"data": {"image": {"url": image_url.tostring}}}]}'

Upvotes: 0

ramesh suthar
ramesh suthar

Reputation: 1

you have to convert the data into JSON and json.dumps() will convert the data into JSON.

data = {"inputs": [{"data": {"image": {"url": image_url}}}]}

json_data = json.dumps(data)

Upvotes: 0

Manish Jain
Manish Jain

Reputation: 1

You need to correct the header as well to get a valid response

header = {'Authorization': 'Key '+ api_key ,"Content-Type": "application/json"}

Upvotes: 0

syntheticgio
syntheticgio

Reputation: 608

Looks like you need to use:

'{"inputs": [{"data": {"image": {"url": "' + image_url + '"}}}]}'

Since the single quotes are creating a string, you can't just add in a variable directly, but need to concatenate it. You were literally sending the text image_url and not the actual value of the variable image_url in that statement.

Upvotes: 0

Related Questions