1
votes

I'm working on a Node.js app that makes request to the Microsoft Graph API to create Azure AD users. Authentication and creating users is working fine. The problem comes when I make a put request to (https://graph.microsoft.com/v1.0/users/{id}/manager/$ref) . I get this error.

StatusCodeError: 400 - "{\r\n  \"error\": {\r\n    \"code\": \"Request_BadRequest\",\r\n    \"message\": \"An unexpected 'EndOfInput' node was found when reading from the JSON reader. A 'StartObject' node was expected.\",\r\n    \"innerError\": {\r\n      \"request-id\": \"6245c337-a157-49a8-9b59-bdd2bf5eea01\",\r\n      \"date\": \"2019-07-17T19:31:20\"\r\n    }\r\n  }\r\n}"
    at new StatusCodeError (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\errors.js:32:15)
    at Request.plumbing.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:104:33)
    at Request.RP$callback [as _callback] (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request-promise-core\lib\plumbing.js:46:31)
    at Request.self.callback (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:185:22)
    at Request.emit (events.js:198:13)
    at Request.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1161:10)
    at Request.emit (events.js:198:13)
    at IncomingMessage.<anonymous> (C:\Users\joshuah\Desktop\msgraph_test\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:286:20)
    at IncomingMessage.emit (events.js:203:15)

Here's the code. I'm using request-promise-native.

 user.assignManager = function(id, manager){
      auth.getToken().then(token => {
        request.put('https://graph.microsoft.com/v1.0/users/' + id + '/manager/$ref', {
            auth: {
                bearer: token
            },
            body :  manager,
            headers: {
                'Content-type': 'application/json'
            }
        }).then(value =>{
            console.log('Assigned Manager');
        }).catch(err =>{
            console.log(err);
        })
    }).catch(err => {
        console.log(err);
    });
}

'manager' that I'm putting in the body

{ 
   "@odata.context":"https://graph.microsoft.com/v1.0/$metadata#directoryObjects/$entity",
    "@odata.type":"#microsoft.graph.user",
    "id":"2db4c4e2-a349-4eb0-97d0-862143f5b01d",
    "businessPhones":[],
    "displayName":"John Smith", 
    "givenName":"John",
    "jobTitle":"Intern", 
    "mail":null,
    "mobilePhone":null,
    "officeLocation":"BR",
    "preferredLanguage":null,
    "surname":"Smith",
    "userPrincipalName":"[email protected]"
}

I also tried like this instead of as a user object.

{
"id": "2db4c4e2-a349-4eb0-97d0-862143f5b01d"
}

I'm assuming its some kind of parsing issue, but I'm at my wits end. Any ideas?

1

1 Answers

1
votes

Got it working.

Add json: true, to the request and the body needs to be

{ "@odata.id": "https://graph.microsoft.com/v1.0/users/{id}" }