Question

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

  • Setting multipart data
  • body instead of form data
  • Setting the content-type
  • Sending as JSON

Things I know are correct

  • authentication --- I'm able to get a correct response if i do a GET req, its just the POST
  • POST path and headers

Link for request-module

Link for github-api

Was it helpful?

Solution

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);
});

OTHER TIPS

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);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top