首页 > 解决方案 > 为什么 github API python POST 请求不起作用?

问题描述

我想使用 API 在 github 上创建一个拉取请求。以下curl命令确实有效

curl \
  -X POST \
  -H "Accept: application/vnd.github.v3+json"  -H "Authorization: token ghp_abc...123" \
  https://api.github.com/repos/alex4200/hello-world/pulls \
  -d '{"head":"copyright_updater","base":"master","title":"Test PR curl"}'

但是这个python片段不起作用

import requests

headers = {
    "Accept": "application/vnd.github.v3+json",
    "Authorization": "token ghp_abc...123"
}
data = {
    "head": "copyright_updater",
    "base": "master",
    "title": "Test PR python"
}

response = requests.post(
    "https://api.github.com/repos/alex4200/hello-world/pulls",
    headers=headers,
    data=data
)
print(response.text)

此命令与 具有完全相同的数据curl,返回 400 错误消息

{"message":"Problems parsing JSON","documentation_url":"https://docs.github.com/rest/reference/pulls#create-a-pull-request"}

这里有什么问题?

标签: pythongithubpython-requests

解决方案


推荐阅读