Service Now - Apache Commons HTTPClient via Javascript

Sunday Feb 9, 2020

Integration can be tricky with SN - here’s a way to use Apache Commons HTTPClient via Javascript to talk to Box:

Code

Here’s the code:

     var boxToken = "Bearer some_token_from_box";
     
     /**
     	JSON format of result.item_collection.entries
       To download file, request https://api.box.com/2.0/files/:file_id/content
     	 {
           "type": "file",
           "id": "637062434801",
           "file_version": {
             "type": "file_version",
             "id": "675529169201",
             "sha1": "ae83734bb68094c6cdefb8d7ca39aed885a316c5"
           },
           "sequence_id": "0",
           "etag": "0",
           "sha1": "ae83734bb68094c6cdefb8d7ca39aed885a316c5",
           "name": "sys_user_group_a063bc10db8b48502c071f8305961964.xml"
         }
     
     */
     
     var client = new Packages.org.apache.commons.httpclient.HttpClient();
     var request = new Packages.org.apache.commons.httpclient.methods.GetMethod("https://api.box.com/2.0/folders/0");
     
     request.addRequestHeader("Authorization", boxToken);
     
     var returnCode = client.executeMethod(request);
     var output = "" + request.getResponseBodyAsString();
     gs.debug("Got response: " + returnCode);
     if(returnCode == 200){
       var result = JSON.parse(output);
       //gs.debug(JSON.stringify(result, null, 2));
       result.item_collection.entries.map( function(f) {
         gs.debug("File name " + f.name + " id is " + f.id);
         return {name: f.name, id: f.id};
       });
     }