GitHub API创建文件 [英] GitHub API to Create a File

查看:80
本文介绍了GitHub API创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想让自己熟悉GitHub API.我正在使用cURL命令来实现其一些基本功能.我可以获得基本的授权&正确创建存储库.目前,我正在尝试使用他们的API&在存储库中创建文件.正在面对消息":未找到"错误作为响应.

So I'm trying to familiarize myself with the GitHub API. I'm using cURL commands to implement some of their basic functionality. 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.

他们的文档建议这样做:

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

我想出了与cURL等效的方法:

I came up with this as the cURL equivalent:

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

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

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.

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

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

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

The repository creation URL I used follows: user/repos as the syntax. Similarly, I've tried using user/repos/repo, but it didn't work.

任何人都可以阐明这一点吗?

Can anyone shed any light on this?

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

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.

感谢TimWolla的回答.

Thanks to TimWolla for the answer.

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

Syntax of a working command to create a file in a repository using the GitHub API:

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

我的例子:

curl -i -X PUT -H'授权:令牌f94ce61613d5613a23770b324521b63d202d5645'-d'{"path":"test4.txt","message":初始提交","committer":{名称:" Neil," email:" neil@abc.com}," content:" bXkgbmV3IGZpbGUgY29udGVudHM =," branch:" master}''https://api.github.com/repos/InViN-test/test_repo1/contents/test4.txt

推荐答案

使用 curl 时,您需要使用 -X 选项:

When using curl you need to specify the correct HTTP Verb (PUT in this case) using the -X option:

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

还使用您的示例有效负载显示了错误500,该缩短的有效负载可以正常工作:

Also using your example payload an error 500 showed up, this shortened payload worked fine:

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

不过,我不知道服务器错误的真正原因.

I don't know the actual reason for the Server error, though.

这篇关于GitHub API创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆