I am trying to create a GitHub repository using AJAX from JQuery, but it is not working, my code is here:
$.ajax({
url: "https://api.github.com/my-user/repos",
headers: { 'Authorization' : 'token my-token' },
method: 'POST',
data:{
"name": "new-repos",
"description" : "New-repos",
"auto_init": true,
"private": false,
},
dataType: 'json',
contentType: 'application/json',
processData: false,
success: function (data, textStatus, request) {
var html_content_header = "";
var obj_header=request.getAllResponseHeaders();
$('#header').html(JSON.stringify(request.getAllResponseHeaders()));
var html_content_text = "";
Object.getOwnPropertyNames(data).forEach(
function (val, idx, array) {
html_content_text=html_content_text+val + ' -> ' + data[val]+ '<br>';
}
);
$('#text').html(html_content_text);
},
error: function(xhr, status, error) {
alert(xhr.status);
}
});
It shows this error in console:
POST https://api.github.com/my-user/repos 404 (Not Found)
And in my error alert:
404
I followed the guide of getting started by GitHub https://developer.github.com/guides/getting-started/, but instead of using CURL, i was using AJAX from JQuery.
Everything was going smoothly, until i tried creating a new repository. I have verified that my token has the correct scopes.
I kind of guess that it does not found the URL.
Is there some kind of limitation using AJAX from JQuery and GitHub?
Am i writing wrong the URL?
The GitHub API for creating a repo is
POST /user/repos
Not "my-user/repo"
See "GitHub API AJAX POST returning 422" for a full example: no trailing ',' in data, and use data: JSON.stringify:
$.ajax({
url: 'https://api.github.com/repos/USERNAME/REPONAME/issues',
type: 'POST',
dataType: 'json',
headers: {
Authorization: 'token MY_PERSONAL_TOKEN'
},
data: JSON.stringify({
"title": "Found a bug",
"description": "Bug description"
}),
success: function (response) {
console.log(response);
}
});