12

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.

Their documentation suggests this:

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

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

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

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?

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.

EDIT: Thanks to TimWolla for the answer.

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

Neil P.
  • 1,026
  • 2
  • 15
  • 32
  • 1
    I had the same problem (404 when trying to create a file, while reading files was no problem at all) and in my case the solution was, that the Personal Access Token needed to have "admin:repo_hooks" in its scopes (additional to users and repos). It took me some time to find this out. – Martin Roob Feb 20 '19 at 06:27

1 Answers1

8

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

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.

TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • TimWolla, what's the actual URL that you used for the PUT command? – Neil P. Mar 11 '14 at 01:42
  • @Neil The same as you did, except for a repository of mine: `https://api.github.com/repos/user/repo/contents/so-test.txt` – TimWolla Mar 11 '14 at 01:49
  • Can you paste the complete command? I'm still getting a "message":"Not Found" error. – Neil P. Mar 11 '14 at 03:06
  • 3
    @Neil Sure: `curl -X PUT -H 'Authorization: token yadayada' -d '{"message": "Initial Commit","content": "bXkgbmV3IGZpbGUgY29udGVudHM="}' https://api.github.com/repos/user/test/contents/so-test.txt` – TimWolla Mar 11 '14 at 03:20
  • Tim, thanks! It works as expected now. I'm using the following command: `curl -i -X PUT -H 'Authorization: ' -d '{"path": "test4.txt", "message": "Initial Commit", "committer": {"name": "", "email": ""}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM=", "branch": "master"}' https://api.github.com/repos/InViN-test/test_repo1/contents/test4.txt` – Neil P. Mar 11 '14 at 03:32
  • I was wondering why there is a `path` in the required parameters. – SOFe Sep 25 '16 at 06:18
  • I suppose if your file is in a different directory than where you're executing? – Neil P. Mar 04 '18 at 17:31