attachmentAPI

rajesh9885
Mega Guru
Hello Can someone point out where am i going wrong
i need to pass an attachment from one Service now system to another gone through other link related to this but could not find anything good
here is the code i am using 

(function sampleRESTMessageV2() {
try {
var user = <username>;
var password = <password>;
var sid = current.sys_id;
var filename = current.file_name;
var filetype = current.content_type;
//var tablename = current.table_name;

var r = new sn_ws.RESTMessageV2();
r.setEndpoint('<instance name>/api/now/attachment/file');
r.setHttpMethod('POST');
r.setBasicAuth(user,password);
r.setStringParameterNoEscape('table_name','incident');
r.setStringParameterNoEscape('table_sys_id',<incident sys_id>);
r.setStringParameterNoEscape('file_name',filename);
r.setStringParameterNoEscape('content-Type',filetype);
r.setRequestBodyFromAttachment(sid);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch (ex) {
var message = ex.getMessage();
gs.log('>>>>>message is >>> ' +message,'rajesh');
}

})();

I am getting response of 400
response body is :{"error":{"message":"Missing parameter: table_name","detail":null},"status":"failure"}
note sure where am i going wrong. I am passing table name, but not sure what needs to be details. I am passing most of the thing in attachment.



Regards

Rajesh



 

4 REPLIES 4

Harsh Vardhan
Giga Patron

try now. 

 

(function sampleRESTMessageV2() {
try {
var user = <username>;
var password = <password>;
var sid = current.sys_id;
var filename = current.file_name;
var filetype = current.content_type;
//var tablename = current.table_name;

var r = new sn_ws.RESTMessageV2();
r.setEndpoint('<instance name>/api/now/attachment/file');
r.setHttpMethod('POST');
r.setBasicAuth(user,password);
r.setQueryParameter('table_name','incident');
r.setQueryParameter('table_sys_id',<incident sys_id>);
r.setQueryParameter('file_name',filename);
r.setRequestHeader('content-Type',filetype);
r.setRequestHeader("Accept", "application/json");
r.setRequestBodyFromAttachment(sid);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
 }
catch (ex) {
var message = ex.getMessage();
gs.log('>>>>>message is >>> ' +message,'rajesh');
} 
 
})();

Sorry,

It still the same

have tried all combination i could think of this would not pass from servicenow,

but when i am passign through postman using binary it works as a charm.

have  you tried to convert those file into payload base64 and then passed it ? 

 

adding one thread here. 

 

https://community.servicenow.com/community?id=community_question&sys_id=988d432ddb9cdbc01dcaf3231f96...

 

 

that does not work either.

what i understand is system is not able to find the location , seems like i have to pass it with binary location.

as in example of curl system.
--data-binary "@<location of the file on file system>" 

I am not good with curl.
any idea how i can pass the value in url

curl <instance name>/api/now/attachment/file?table_name=incident&table_sys_id=<sysid>&file_name=<attachment name>" 

if any one has done this using javascript please do share