Danielle
Danielle

Reputation: 181

POST to GitHub v3 API using ajax and JavaScript fails with a HTTP 404

I am trying to post a blob to the GitHub API in JavaScript / jQuery as per the docs https://developer.github.com/v3/git/blobs/#create-a-blob - however I keep receiving a 404 request

My current request in JavaScript looks like so:

var uploadURL ="https://api.github.com/repos/daniellevass/web-quiz/git/blobs" + accessToken;

console.log(uploadURL);

$.ajax({
  type: "POST",
  dataType: "jsonp",
  url: uploadURL,
  data:{
      "content": "my message",
      "encoding": "utf-8"
    }
})
  .done(function( data ) {
    console.log( data );
  });

The console spits out the following URL (the access token is correct):

https://api.github.com/repos/daniellevass/web-quiz/git/blobs?access_token=xxx

I get the following response:

data: Object
    documentation_url: "https://developer.github.com/v3"
    message: "Not Found"
    __proto__: Object
meta: Object
   X-Accepted-OAuth-Scopes: "repo"
   X-GitHub-Media-Type: "github.v3"
   X-OAuth-Scopes: "gist, repo, user"
   X-RateLimit-Limit: "5000"
   X-RateLimit-Remaining: "4992"
   X-RateLimit-Reset: "1401550358"
   status: 404
   __proto__: Object
__proto__: Object

The response shows in the meta object, that I have asked requested the correct OAuth scopes. I can also successfully GET any data using the GitHub api and ajax - such as a list of the users repositories.

I suspect I may have something incorrect with my POST request, but I have no idea where I've gone wrong so any help or advice would be much appreciated!

Thank you,

Danielle.

Upvotes: 1

Views: 2083

Answers (1)

Ivan Zuzak
Ivan Zuzak

Reputation: 18782

You can't use JSONP to make non-GET requests.

However, the good news is that you don't need to use JSONP since the API supports CORS.

Give this a try:

var uploadURL ="https://api.github.com/repos/daniellevass/web-quiz/git/blobs" + accessToken;

console.log(uploadURL);

$.ajax({
  type: "POST",
  url: uploadURL,
  contentType: "application/json",
  dataType: "json",
  data: JSON.stringify({
      "content": "aGVsbG8=",
      "encoding": "utf-8"
    })
})
  .done(function( data ) {
    console.log( data );
  });

Upvotes: 3

Related Questions