Search code examples
node.jsazurerestifyazure-ad-graph-api

Updating user using azure-graphapi module for Node js


I'm trying out to update an user on my Azure account using Node.js technology. I'm using azure-graphapi module to send request and initialize. Following is my code.

var GraphAPI = require('azure-graphapi');
var graph = new GraphAPI(appSettings.oauthOptions.tenantId, appSettings.oauthOptions.clientId, appSettings.oauthOptions.clientSecret);
var reqHeaders = { "content-type": "application/json" };
var reqBody = {
            "department": "Sales",
            "usageLocation": "US"
        }
    var person = {
        userId: userID
    };

graph.patch('users/f0eceb4f-xxxx-409a-xxxx-4e3exx4e3157', JSON.stringify(reqBody), reqHeaders, function (err, user) {
        if (!err) {
            console.log(user);
        }
        else {
            console.log(err);
        }
    });

Even after providing content-Type header it is throwing me an error as "{ [Error: Graph API Error: 400 (Bad Request) Content-Type header value missing.] statusCode: 400 }"

It would be of great help if any one can help me out on this issue.


Solution

  • There several mistakes in this module you are using. To make the code work, we should do some additional modifications in the source code GraphAPI.js in node_modules/auzre-graphapi:

    Start with Line 195, there is an if condition stmt, the author seems forget to define the content which is used since line 199, and only if you have to parse the post body to buffer object, it will set the content-type header. So, we can quickly modify the code as:

    if (data) {
            if (Buffer.isBuffer(data)) {
                options.headers['Content-Type'] = contentType;
            } else if (!contentType) {
                content = data;
                if (typeof content === 'string') {
                    options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
                    options.headers['Content-Length'] = content.length;
                } else if (content !== null && typeof content === 'object') {
                    content = JSON.stringify(content);
                    options.headers['Content-Type'] = 'application/json';
                    options.headers['Content-Length'] = content.length;
                }
            } else {
                if (typeof contentType === 'string') {
                    options.headers['Content-Type'] = contentType;
                } else if (contentType['Content-type'] !== null) {
                    options.headers['Content-Type'] = contentType['Content-type'];
                }
            }
        }
    

    And then set the header as your code: var reqHeaders = { "Content-type": "application/json" };

    BTW, as the update user document refers, it will response 204 without response body if success, so your code will print "undefined" if success.

    update 4/19/2016

    As the author doesn't maintenance the package any more, and he has created a new package graph-service for universal Graph APIs. Refer to https://github.com/fhellwig/azure-graphapi/issues/5#issuecomment-211392546