Search code examples
node.jsrestzendeskzendesk-api

PUT requests not working on Server side (node js)


I am trying to make a PUT request in Node JS to update the "value" field. Here is my code:

var identityJson = {};
identityJson.identity = {};
identityJson.identity.value = "+61111111111";
var url = "https://[Zendeskdomain].zendesk.com/api/v2/users/[user ID]/identities/[Identity ID].json";
var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
        if (this.readyState==4 )
        {
            console.log(this.status)
            console.log(this.responseText);
        }
    }

request.open('PUT', url, true);
request.setRequestHeader("Authorization", "Bearer " + token);
request.setRequestHeader('Content-type', 'application/json');
request.send(JSON.stringify(identityJson));

This script returns 200 as the status and prints the identity as it was before. In this case, the value is still "+37011111111"

200
{"identity":{"url":"https://[Zendeskdomain].zendesk.com/api/v2/users/[user ID]/identities/[Identity ID].json","id":[Identity ID],"user_id":[user ID],"type":"phone_number","value":"+37011111111","verified":true,"primary":true,"created_at":"2018-09-07T12:24:02Z","updated_at":"2018-09-07T12:24:02Z"}}

If I send the same request on client side (putting the script on a simple webpage), the value does change so it seems that the information entered is correctly formatted.

Anybody has had similar issues (in Zendesk or not) while submitting a PUT request from Node JS and can point me in the right direction?

Note: [Zendeskdomain], [user ID] & [Identity ID] are irrelevant for this so I removed them, however they are correctly entered in the script.

Thanks!


Solution

  • You're in server side, don't use XMLHttpRequest, it's a javascript client side function...

    Use a package like request : https://www.npmjs.com/package/request

    Then just do :

    request({
      method: "PUT",
      uri: "http://sdfsfdsdf",
      json: body
     }, function(err, response, body){
        ....
    });