Generating custom XML file as a record in ServiceNow

Ravish Shetty
Tera Guru

Hi all,

 

I am looking to create an custom XML file and attach it to a RITM. I am using the Attachment API. it works fine in a third party client like Postman but when i use the below code 

 

 

 

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://test.service-now.com/api/now/attachment/file?table_name=sc_req_item&table_sys_id=TEST&file_name=test.xml');
request.setHttpMethod('POST');
var user = 'test';
var password = 'test';
request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/xml");
request.setRequestHeader("Content-Type", "*/*");
request.setRequestBody("<note><to>Tove</to><from>Dave</from> <heading>Reminder</heading> <body>Test</body></note>");
var response = request.execute();
gs.log(response.getBody());gs.log(response.getStatusCode());

 

 

 

 

This is the Output I get:

 

*** Script: {"error":{"message":"Invalid file type: */*","detail":null},"status":"failure"}
*** Script: 400

I tried a few different content types but did not seem to work.

Please advice how can I fix this.

 

Thanks,

Ravish

3 REPLIES 3

Satishkumar B
Giga Sage
Giga Sage

Hi @Ravish Shetty 
The issue with your code is related to the Content-Type header in the request. For file uploads via the Attachment API , the Content-Type should be set to application/xml if you're uploading an XML file.

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://test.service-now.com/api/now/attachment/file?table_name=sc_req_item&table_sys_id=TEST&file_name=test.xml');
request.setHttpMethod('POST');
var user = 'test';
var password = 'test';
request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/xml");
request.setRequestHeader("Content-Type", "application/xml"); // Correct Content-Type
request.setRequestBody("<note><to>Tove</to><from>Dave</from> <heading>Reminder</heading> <body>Test</body></note>");
var response = request.execute();
gs.log(response.getBody());
gs.log(response.getStatusCode());

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

Thanks Satishkumar but getting this message now

 

*** Script: {"error":{"message":"Invalid file type: application/xml","detail":null},"status":"failure"}
*** Script: 400

 

Below is the code I executed:

 

 

 

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://test.service-now.com/api/now/attachment/file?table_name=sc_req_item&table_sys_id=test&file_name=test.xml');
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'test';
var password = 'test@';
request.setBasicAuth(user,password);
request.setRequestHeader("Accept", "application/xml");
request.setRequestHeader("Content-Type", "application/xml"); // Correct Content-Type
request.setRequestBody("<note><to>Tove</to><from>Dave</from> <heading>Reminder</heading> <body>Test</body></note>");
var response = request.execute();
gs.log(response.getBody());gs.log(response.getStatusCode());

 

 

 

 

 

 

 

@Ravish Shetty  the server is rejecting the application/xml content type. Check the below image from the doc

SatishkumarB_0-1722370680943.png

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................