1

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 Answer 1

1

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

2 Comments

Don't forget to accept your answer once you're able to!
I have the same issue, but unable to move beyond the 400 error.. I tried have 'json':'true' in the headers as well as the body.. Content-type is already in place.. body is formatted exactly like shown above.. so the URI contains the {id} of the user and body contains {id} of the manager, correct?

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.