2

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

2 Answers 2

2

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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

I got the answer, instead of sending as a form, send it as json.
Could you provide an answer with a code snippet, for other people who are interested?
@ChrisAlexander look at the other answer

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.