如何将此Curl转换为Python请求

时间:2014-09-18 06:28:58

标签: python-2.7

卷曲命令

curl --user "github" --request POST --data '{"description":"Created via API","public":"true","files":{"587":{"content":"adasdaskdasjsadsasdjfcsdjsdjcsdjcsdjcsdjcfsdjsdjfjsdfjsdfsdfsjdfmdsfdsf"}}' https://api.github.com/gists

然后要求输入密码并创建gist。

这会产生JSON错误:

url = 'https://api.github.com/gists'
auth = ('github','password')
requests.post(url,auth=auth,data={
  "description": "the description for this gist",
  "public": False,
  "files": {
    "file1.txt": {
      "content": "String file contents"
    }
  }
})

错误:{"message":"Problems parsing JSON","documentation_url":"https://developer.github.com/v3"}

我的请求出错的任何想法?

1 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但如果有人像我一样偶然发现:

如果您使用的是版本2.4.2或更新版本,则应该可以使用requests.post(url, json=<your dictionary>)

有关详细信息,请参阅此答案:Python Request Post with param data