0

我是 python 请求的新手,不知道如何使用发布请求调用此 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: a bytes-like object is required, not 'str'

4

1 回答 1

2

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

data = b'https://upload.wikimedia.org/whatevertheurlis.jpg'
于 2021-10-26T14:09:44.077 回答