2

This seems to be answered elsewhere but using the same command as other answers I still get a 404.

I'm trying to test creating a file with GitHub's v3 API. Whether I use curl or a rest api tester I get a 404 "not found" error. I believe I am getting properly authorized as I can check my rate count and it is counting down from 5,000 (the rate limit you get when authorized).

I can do a GET like so:

curl -X GET -H "Authorization: token <mytoken>" https://api.github.com/repos/<myorg>/<myrepo>/contents/testfile

My PUT is like so:

curl -X PUT -H "Authorization: token <mytoken>" https://api.github.com/repos/<myorg>/<myrepo>/contents/newfile -d "{'message': 'Initial Commit','content': 'bXkgbmV3IGZpbGUgY29udGVudHM='}"

I've also tried this:

curl -X PUT -H "Authorization: token <mytoken>" https://api.github.com/repos/<myorg>/<myrepo>/contents/test.txt -d "{'path': 'test.txt', 'branch': 'dev', 'message': 'Initial Commit', 'committer': {'name': '<myname>', 'email': '<myemail>'}, 'content': 'bXkgbmV3IGZpbGUgY29udGVudHM='}"

So to recap, GET seems to work. PUT seems to not work. If anyone can help please do. If I get no answer, someone please tell my story.

4
  • Your payload is invalid JSON, and trying to use it gives me a 400 error, not a 404. Is this actually what you tried to PUT? Commented Jul 20, 2017 at 21:34
  • What is invalid about it? I pulled the json into Visual Studio and it looked fine. I will say that technically the single quotes should be double quotes but when working with curl on windows I've always had to place any internal quotes to single. To answer, yes this is exactly why I used (while obviously replacing any <> with real values) and I get a 404 every time. Also, it's worth reiterating, I get the same result using a REST tester like ARC. Commented Jul 20, 2017 at 22:10
  • It's invalid because the single quotes must be double quotes in order for the payload to actually be JSON. The curl command works fine for me when I fix that part. If even invalid JSON is giving you a 404, then my only remaining guess is that https://api.github.com/repos/<myorg>/<myrepo>/contents doesn't actually exist, either because the repo doesn't exist or just because you misspelled something. Commented Jul 20, 2017 at 22:28
  • @jwodder: See my answer below. Btw, when I started using a write-enabled user, I got a 400 as you did. I did get curl working with the json when I escaped all the quotes which is a pain which is why I tried using ARC first but the 404 threw me. Thanks for your help! It actually did steer me to find the underlying problem so it is much appreciated. Commented Jul 20, 2017 at 22:57

2 Answers 2

4

Turns out, I'm just a moron. I was using the personal access token (PAT) of a user that had read-only access to my repo. Using a PAT of someone that had write access worked just fine. Who knew?

Sorry about that, internet. I'll never doubt you again.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
IMO this isn't really your fault, they should fix the API to make the error message more clear.
they do 404 on purpose so to disclose as little information as possible
Same issue but was updating an action workflow file. In order for that to work you also need the workflow permission on the token
0

I was creating an API wrapper and my input path had an extra leading "/" I removed the forward slash and no more error 404.

So, look for typos in the request URL, especially if your path constructor places a trailing slash. Good constructors usually take care of this case, but clearly not all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.