如何执行这个python post请求



我是python请求的新手,不知道如何使用post请求调用此API。这是我得到的信息:

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

目前我得到的是:

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)

但是我得到这个错误:TypeError:需要bytes类对象,而不是'str'

此API接受图像URL或base64编码的图像。目前,data被定义为一个集合。修复非常简单:

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

最新更新