简体   繁体   English

用于创建文件的GitHub API

[英]GitHub API to Create a File

So I'm trying to familiarize myself with the GitHub API. 所以我正在尝试熟悉GitHub API。 I'm using cURL commands to implement some of their basic functionality. 我正在使用cURL命令来实现它们的一些基本功能。 I can get the basic authorization & repository creation correctly. 我可以正确地获得基本授权和存储库创建。 Currently, I'm trying to create a file in a repository using their API & am facing the "message":"Not Found" error as the response. 目前,我正在尝试使用他们的API在存储库中创建一个文件并且面对“消息”:“未找到”错误作为响应。

Their documentation suggests this: 他们的文件表明:

PUT /repos/:owner/:repo/contents/:path

I came up with this as the cURL equivalent: 我想出了这个作为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

I think the problem is with the API URL I'm using at the end, but I can't seem to figure out what the URL should look like. 我认为问题在于我最后使用的API URL,但我似乎无法弄清楚URL应该是什么样子。

This is what I used to create a repository: 这是我用来创建存储库的东西:

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

The repository creation URL I used follows: user/repos as the syntax. 我使用的存储库创建URL如下:user / repos作为语法。 Similarly, I've tried using user/repos/repo, but it didn't work. 同样,我尝试过使用user / repos / repo,但它没有用。

Can anyone shed any light on this? 任何人都可以对此有所了解吗?

I've gone through various StackOverflow questions & many seem similar but none really offer an example so I can figure out where the mistake lies. 我已经完成了各种StackOverflow问题,很多看似相似,但没有一个真正提供一个例子,所以我可以找出错误所在。

EDIT: Thanks to TimWolla for the answer. 编辑:感谢TimWolla的回答。

Syntax of a working command to create a file in a repository using the GitHub API: 使用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>

My example: 我的例子:

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

When using curl you need to specify the correct HTTP Verb ( PUT in this case) using the -X option: 使用curl您需要使用-X选项指定正确的HTTP谓词(在本例中为PUT ):

curl -X PUT -H 'Authorization: …' yadayada

Also using your example payload an error 500 showed up, this shortened payload worked fine: 同样使用你的示例有效负载出现错误500,这个缩短的有效负载工作正常:

{"message": "Initial Commit","content": "bXkgbmV3IGZpbGUgY29udGVudHM="}

I don't know the actual reason for the Server error, though. 但是,我不知道服务器错误的实际原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM