GitHub API创建文件

Nei*_*eil 5 git curl github http-put github-api

因此,我试图使自己熟悉GitHub API。我正在使用cURL命令来实现其一些基本功能。我可以正确获取基本授权和存储库创建。当前,我正在尝试使用其API在存储库中创建文件,并且面临“ message”:“ Not Found”错误作为响应。

他们的文档表明:

PUT /repos/:owner/:repo/contents/:path
Run Code Online (Sandbox Code Playgroud)

我想出了这个作为cURL等效项:

curl -H 'Authorization: <token>' -d '{"path": "test.txt", "message": "Initial Commit", "committer": {"name": "<name>", "email": "<email>"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM=", "note":"Test Commit"}' https://api.github.com/repos/InViN-test/test_repo1/contents/test.txt
Run Code Online (Sandbox Code Playgroud)

我认为问题出在我最后使用的API URL,但我似乎无法弄清楚URL的外观。

这是我用来创建存储库的内容:

curl -i -H 'Authorization: <token>' -d '{"name": "test_repo1", "message": "Initial Commit", "committer": {"name": "<name>", "email": "<email>"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM=", "note":"Test Commit"}' https://api.github.com/user/repos
Run Code Online (Sandbox Code Playgroud)

我使用的存储库创建URL如下:user / repos作为语法。同样,我尝试使用user / repos / repo,但是没有用。

谁能对此有所启示?

我经历了各种StackOverflow问题,其中许多似乎相似,但没有一个真正提供示例,因此我可以找出错误的出处。

编辑:感谢蒂姆沃尔拉的答案。

使用GitHub API在存储库中创建文件的有效命令语法:

curl -i -X PUT -H 'Authorization: token <token_string>' -d '{"path": "<filename.extension>", "message": "<Commit Message>", "committer": {"name": "<Name>", "email": "<E-Mail>"}, "content": "<Base64 Encoded>", "branch": "master"}' https://api.github.com/repos/<owner>/<repository>/contents/<filename.extension>

我的例子:

curl -i -X PUT -H 'Authorization: token f94ce61613d5613a23770b324521b63d202d5645' -d '{"path": "test4.txt", "message": "Initial Commit", "committer": {"name": "Neil", "email": "neil@abc.com"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM=", "branch": "master"}' https://api.github.com/repos/InViN-test/test_repo1/contents/test4.txt

Tim*_*lla 7

使用时,curl您需要PUT使用以下-X选项指定正确的 HTTP 动词(在本例中):

curl -X PUT -H 'Authorization: …' yadayada
Run Code Online (Sandbox Code Playgroud)

同样使用您的示例有效负载出现错误 500,这个缩短的有效负载工作正常:

{"message": "Initial Commit","content": "bXkgbmV3IGZpbGUgY29udGVudHM="}
Run Code Online (Sandbox Code Playgroud)

不过,我不知道服务器错误的实际原因。

  • @Neil Sure:`curl -X PUT -H '授权:令牌 yadayada' -d '{"message": "Initial Commit","content": "bXkgbmV3IGZpbGUgY29udGVudHM="}' https://api.github.com/回购/用户/测试/内容/so-test.txt` (3认同)