Bernardo
Bernardo

Reputation: 86

How to perform this python post request

I am new to python requests and do not know how to call this API with a post request. This is the information I got:

POST /whatcaristhat?key=<CarsXE_API_Key> HTTP/1.1              
Host: http://api.carsxe.com             
Content-Type: text/plain             
https://upload.wikimedia.org/wikipedia/commons/4/44/2019_Acura_RDX_A-Spec_front_red_4.2.18.jpg

What I got so far is:

import requests

api_key = 12345
url = f'http://api.carsxe.com/whatcaristhat?key={api_key}'
data = b'https://upload.wikimedia.org/wikipedia/commons/4/44/2019_Acura_RDX_A-Spec_front_red_4.2.18.jpg'

headers = {'Content-Type' : 'text/plain'}
r = requests.post(url,
        data=data,  
        headers=headers)
print(r.status_code)

But I get this error: TypeError: a bytes-like object is required, not 'str'

Upvotes: 0

Views: 1255

Answers (1)

Marat
Marat

Reputation: 15738

This API takes either an image URL or a base64 encoded image. As of now, data is defined as a set. The fix is pretty straightforward:

data = b'https://upload.wikimedia.org/whatevertheurlis.jpg'

Upvotes: 2

Related Questions