2

I'm learning REST api for github and trying to create a new repository by running a JS. Here is my function for creating a new repo: the token is generated, and all access/scopes is granted for my token.

function createNewRepo(userId, name){

    var options = {
        url: urlRoot + "/user/repos",
        method: 'POST',
        headers: {
            "User-Agent": "EnableIssues",
            "content-type": "application/json",
            "Authorization": token,
            "name": name,
            "description": "This is repo creating by REST",
            "homepage": "https://github.com",
            "private": false,
            "has_issues": true,
            "has_projects": true,
            "has_wiki": true,

        }
    };
    //console.log(eval(options));


    request(options, function (error, response, body) 
    {
        var obj = JSON.parse(body);
        console.log( obj );
    });

}

however when running this, I find,

{ message: 'Problems parsing JSON', documentation_url: 'https://developer.github.com/v3' }

I'm not sure how exactly could the JSON be invalid.

Also, the documentation says it must include public_repo or repo which I'm also not sure how to apply here.

3
  • What json? You neglect to include the json you're sending. Commented Sep 3, 2017 at 13:23
  • I'm really not sure. I'm new to javascript. I thought the options are being sent to github as json object. Whatever the case, the message returns "problem parsing json" while running this function ! @DaveNewton Commented Sep 3, 2017 at 13:26
  • 1
    They are, but you've put everything into the headers, and nothing in the body. Commented Sep 3, 2017 at 15:04

1 Answer 1

2

This for me has created the repository.

var myToken = "token INSERTHERETHETOKEN"; // <-- there must be the word token before the actual token.
var urlRoot = "https://api.github.com";
var request = require("request");

function createNewRepo(user, name, token){
  var options = {
    url: urlRoot + "/user/repos",
    method: 'POST',
    headers: {
      "User-Agent": user,
      // "content-type": "application/json", This is set by json:true
      "Authorization": token,
      "Accept": "application/vnd.github.v3+json"
    },
    body: { 
      "name": name,
      "description": "This is repo creating by REST",
      "homepage": "https://github.com",
      "private": false,
      "has_issues": true,
      "has_projects": true,
      "has_wiki": true
    },
    json: true 
  };

  request(options, function (error, response, body) {
    // Do your stuff... but notice that response
    // is already a json object and you don't need to parse it.
  });
}

createNewRepo("YourGithubUser", "test.repository", myToken);
Sign up to request clarification or add additional context in comments.

5 Comments

this time it gives: undefined:1 [object Object] ^ SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at Request._callback,..............
I've tested the code. The error is raised by the line of JSON.parse, in your callback.
oh thank you very much. I don't have much experience working with json object. pardon my ignorance !
Yes: response is ALREADY a js object, so you don't need to parse it, and if you try it fails.
Don't worry, asking questions is why SE exists. Glad that it helped you

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.