What's raw data in REST Attachment API

Gaurav Bajaj
Kilo Sage

Hi

I am in middle of posting an attachment( lets says an image of jpeg format) with Rest Attachment API. I am confused about the raw data to be used in the request,

I have been trying it with Rest API Explorer and unable to find what is supposed to be added in raw data.

I don't want to upload a file directly through Binary data option as I am getting the data in base64 format from a 3rd party.

PS: I also don't want to use ECC-queue function to add an attachment.

find_real_file.png

Any light on it is highly appreciated.

Thanks

Gaurav

1 ACCEPTED SOLUTION

Hi Andrew



I got it working as I was calling the table API and not attachment API here.


Took reference from the below link.



https://community.servicenow.com/thread/266337#1153136




Thanks for your time.


View solution in original post

9 REPLIES 9

Hi Andrew,



I want to achieve it through ServiceNow itself.


I have an image with me which I am trying to post it to another SNOW instance, I can't upload an image as I want to do it programmatically.


I was able to achieve it with Attachment Creator with ECC QUEUE method. Here I am looking for an alternative of achieving the same with attachment API.


Ah ok,



Try the setRequestBodyFromAttachment function of RESTMessageV2:



https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=r_RMV2-setReqBodyFromAttachment_S



This should allow you to send an attachment straight to the Attachment API.



Andy


Thanks Andrew for guiding me through.



Now, I am trying to create a ticket from one instance to another with an attachment as well.


But the receiving instance is not taking the request body now and throwing error while reading the request.


My doubt is that the request body is not properly typecast into string/base64.



Info Message {"error":{"detail":"Cannod decode: java.io.StringReader@102d59","message":"Exception while reading request"},"status":"failure"}





(function executeRule(current, previous /*null when async*/) {



// Add your code here






var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://example.service-now.com/api/now/table/incident?sysparm_fields=number');


request.setHttpMethod('POST');



//Eg. UserName="admin", Password="admin" for this code sample.


var user = 'admin';


var password = 'admin';



request.setBasicAuth(user,password);


request.setRequestHeader("Accept","application/json");


request.setRequestHeader('Content-Type','application/json');


request.setRequestBody("{\"short_description\":\"This is test ticket created from rest message API\",\"description\":\"This is test ticket created from rest         message API\"}");




//get attcahment sysyId


      var atttchSysId = getAttcahmentId(current);


gs.addInfoMessage(atttchSysId);


if(atttchSysId!=''){


  request.setRequestBodyFromAttachment(atttchSysId);


}


gs.addInfoMessage("request body is "+request.getRequestBody());



var response = request.execute();


gs.addInfoMessage(response.getBody());





})(current, previous);






function getAttcahmentId(currentObj){




var gr = new GlideRecord('sys_attachment');


gr.get('table_sys_id',currentObj.sys_id);



return gr.sys_id.toString();



}


Hi Andrew



I got it working as I was calling the table API and not attachment API here.


Took reference from the below link.



https://community.servicenow.com/thread/266337#1153136




Thanks for your time.


Chandu Telu
Tera Guru
Tera Guru

Hi Gaurav,



you want to check the functinality works in Rest Api explorer


https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/integrate/inbound-rest/concept/...



if you want to integrate with third party


Attachment API



Please provide your feedback appropriately (Like, Helpful, Endorse AND/OR Correct) to help community.



Thanks


Chandu Telu