Jackson Geller
Jackson Geller

Reputation: 189

Github API error when creating repo from CL with node, JSON parsing error

Creating a node CLI to create repos from CL, having an issue posting to github api. I'm using the request module to post to the github API.

request.post({
    url: 'https://api.github.com/user/repos',
    headers:{
        'User-Agent': 'git CL - node', 
        'Content-type': 'application/json'
    },
    auth:{
        username: '-username-',
        password: '-password-'
    }, 
    form:{ 
        name: "a-new-repo" 
    }

}, function(err, res, body){
    console.log(body);
});

The error I'm getting is {"message":"Problems parsing JSON","documentation_url":"http://developer.github.com/v3"}

I've tried a ton of things, like

Things I know are correct

Link for request-module

Link for github-api

Upvotes: 2

Views: 500

Answers (2)

aredridel
aredridel

Reputation: 1777

Set json to the data you want to send, not form:

request.post({
    url: 'https://api.github.com/user/repos',
    headers:{
        'User-Agent': 'git CL - node', 
        'Content-type': 'application/json'
    },
    auth:{
        username: '-username-',
        password: '-password-'
    }, 
    json:{ 
        name: "a-new-repo" 
    },
}, function(err, res, body){
    console.log(body);
});

Upvotes: 2

Chris Alexander
Chris Alexander

Reputation: 1302

There is a parameter called json in the API docs, have you tried setting that to be true?

From my reading, this is what is necessary to send the form data as a JSON structure, rather than as form-encoded in the request body.

request.post({
    url: 'https://api.github.com/user/repos',
    headers:{
        'User-Agent': 'git CL - node', 
        'Content-type': 'application/json'
    },
    auth:{
        username: '-username-',
        password: '-password-'
    }, 
    form:{ 
        name: "a-new-repo" 
    },
    json: true

}, function(err, res, body){
    console.log(body);
});

Upvotes: 0

Related Questions