0

For a homework (in Signavio Workflow Accelerator) I need to add users to an organization on Github using the GitHub-API v3. The Code has to be written in JavaScript which i a language I'm not very familiar with.

At the moment I get the following error code: "SyntaxError: Unexpected token o in JSON at position 1 at Request._callback". So I have the feeling that there might be a problem with the parsing.

var link = 'https://api.github.com/orgs/myorganization/memberships/' + githubUser
var token = 'mytoken'

request({url: link, method: 'put', headers: {'User-Agent': 'request'}, auth: {username: token}, JSON: true},
  function (response, body) {
    console.log(body)
    if(body !== undefined){
      body = JSON.parse(body)
      body['state'][0]['main']
      status = body['main']['state']
      status = body.main.state
    }
    else{
      status = 'error'
    }
  })

I don't know if this might be helpful, but if I perform this put request using cURL it works and the answer starts with:

    {
      "url": "https://api.github.com/orgs/myorganization/memberships/githubUser",
      "state": "pending",
  ...}

So this "state" is the value I want to read in the code above.

Already thanks for helping!

spider
  • 1

1 Answers1

0

I worked together with a friend of mine and together we found a working solution. So if anyone else is having the same struggle: This piece of code does the magic!

var link = 'https://api.github.com/orgs/myorganization/memberships/' + githubUser
var token = 'mytoken'
  const options = {
      url: link,
      method: 'put',
      headers: {'User-Agent': 'request'}, auth: {username: token}
  }
  function callback(error, response, body) {
    console.log(error)
    if(!error && response.statusCode == 200){
      const info = JSON.parse(body)
      status = info['state'][0]['main']
      console.log(status)
      status = info['state']
      status = info.state
    }
    console.log(body)
  }
  request(options, callback)
spider
  • 1