首页 > 解决方案 > 在python中请求GET后得到400个错误请求

问题描述

我想将此 curl 命令转换为 python 脚本

curl -v -H "Content-Type: application/json" -H "Authorization: Bearer MYTOKEN" https://www.zopim.com/api/v2/chats

我在下面编写了python脚本,但我得到<Response [400]>并且不起作用。

import requests

url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"

response = requests.post(url,
      headers={"Content-Type":"application/json;charset=UTF-8",
               "Authorization": "Bearer {}".format(access_token)})
print(response)

任何建议将不胜感激。谢谢。

标签: pythongetrequestzendesk

解决方案


您应该使用requests.get而不是requests.post,因为您想要的是 GET 请求:

import requests

url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"

response = requests.get(url,
      headers={"Content-Type":"application/json;charset=UTF-8",
               "Authorization": "Bearer {}".format(access_token)})
print(response)

推荐阅读