Http在海报上取得了成功,但在python中失败了

时间:2015-02-07 17:22:12

标签: python http github-api

在下面的代码中,我正在尝试使用http post创建一个存储库,但是我总是得到400个错误请求,当我发送带有海报的http帖子时,我创建了201,这段代码出了什么问题?

token = raw_input('Access Token: ')
url = 'https://api.github.com/user/repos?access_token=' + token
values = {"name":"newnewnewnew"}
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = urllib2.urlopen(req)
the_page = response.read();
print the_page

海报:

enter image description here

1 个答案:

答案 0 :(得分:1)

根据the GitHub API v3 documentation,对于POST请求,参数应使用 json 进行编码,内容类型应为application/json

import json

....

token = raw_input('Access Token: ')
url = 'https://api.github.com/user/repos?access_token=' + token
values = {"name": "newnewnewnew"}
data = json.dumps(values)  # <---
req = urllib2.Request(url, data, headers={'Content-Type': 'application/json'})  # <---
response = urllib2.urlopen(req)
the_page = response.read()
print the_page