python - 将 'boxes' 与 imgflip 的 API 一起使用时出现错误消息

标签 python python-requests

我正在使用 Python 的请求模块通过 imgflip 的 API“生成 Meme”,但是在使用 boxes 时收到以下错误消息:

未指定文本。请记住,API 请求参数是 http 参数而不是 JSON。

这是我当前的代码:

boxes = [{"text": top}, {"text": bottom}]

params = {
       "template_id": 181913649,
       "username": username,
       "password": password,
       "font": "arial",
       "boxes": boxes
}

meme = requests.post(url="https://api.imgflip.com/caption_image", params=params).json()

我已将格式化为他们希望的方式,如他们的文档中所述( https://imgflip.com/api ) 所以我不知道为什么它不起作用......

最佳答案

目前您正在发送如下内容:

https://api.imgflip.com/caption_image?template_id=181913649&username=username&password=password&font=arial&boxes=boxes

我相信这应该是 application/x-www-form-urlencoded编码:

params = {
    "template_id": 181913649,
    "username": "username",
    "password": "password",
    "font": "arial",
    "boxes[0][text]": "top",
    "boxes[0][color]": "#ffffff",
    "boxes[1][text]": "bottom",
    "boxes[1][color]": "#ffffff",
}

所以你应该也可以使用 data (data=params) 参数代替。

关于python - 将 'boxes' 与 imgflip 的 API 一起使用时出现错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66807028/

相关文章:

python - 使用 'Requests' python 模块进行 POST 请求,接收响应就好像它是 GET

python - 使用 python-requests 从 DHL 获取跟踪详细信息

python - set 中的哈希表在 python 中如何工作?

python - peewee:classname_set 为 json 数组

python - 从网站上显示的图表中获取数据

python - 使用 Requests Post 登录此站点无效

python - Instagram 私有(private) API 403,即使有 cookie 和 header

python - 在 sqlalchemy 中重新排序复合主键

python - Pandas Dataframe - 根据索引位置计算值

python - ConnectionError,最大重试次数超过 url,(由无引起)