Generating custom XML file as a record in ServiceNow

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 11:58 AM - edited 07-30-2024 12:11 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 12:52 PM
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.
…………………………………………........................................................................................

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 01:06 PM - edited 07-31-2024 07:35 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2024 01:18 PM
@Ravish Shetty the server is rejecting the application/xml content type. Check the below image from the doc
…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution ✅!! If this helps you to understand.
…………………………………………........................................................................................