- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2025 05:53 AM
Hi,
Can we able to attach a file from the postman same as OOB table api from the scripted web services ?
The below screenshot is of the sending attachment by using the OOB table api.
But for the custom scripted web services, we are send request in JSON body as below but not in multipart/form-data.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 12:22 AM
it's not able to read the file data hence went inside No file uploaded
You can check the links I shared above, some solution might be present there
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2025 07:17 PM
Please close the thread by marking appropriate response as correct so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 12:00 AM - edited 01-22-2025 12:01 AM
Hi @Ankur Bawiskar ,
Previously used to get error response as invalid request format. But after adding multipart/form-data in Supported Request Formats as below not getting error.
But getting error response as below
(function process(request, response) {
// Log request details for debugging
gs.info('# Request received. Query Params: ' + JSON.stringify(request.queryParams));
gs.info('# Request Headers: ' + JSON.stringify(request.headers));
// Get the table name and sys_id from the request
var tableName = request.queryParams.table_name;
var tableSysId = request.queryParams.table_sys_id;
gs.info('# Table Name: ' + tableName + ', Table Sys ID: ' + tableSysId);
// Validate table name and sys_id
if (!tableName || !tableSysId) {
gs.error('# Table name or table sys_id not provided.');
response.setStatus(400);
response.setBody({
status: "Failure",
message: "Table name and table sys_id are required."
});
return;
}
// Check if a file was uploaded
if (!request.files || request.files.length === 0) {
gs.error('# No file uploaded.');
response.setStatus(400);
response.setBody({
status: "Failure",
message: "No file was uploaded."
});
return;
}
// Get the first file from the request
var uploadedFile = request.files[0];
var fileName = uploadedFile.fileName; // Name of the uploaded file
var fileContentType = uploadedFile.contentType; // MIME type of the file
var fileData = uploadedFile.getBytes(); // Binary content of the file
gs.info('# Uploaded File - Name: ' + fileName + ', Content-Type: ' + fileContentType);
// Fetch the record using GlideRecord
var gr = new GlideRecord(tableName);
if (gr.get(tableSysId)) {
// Record found, proceed to attach the file
gs.info('# Record found with Sys ID: ' + tableSysId + '. Attaching file...');
var attachment = new GlideSysAttachment();
attachment.write(gr, fileName, fileContentType, fileData);
// Respond with a success message
gs.info('# File successfully uploaded to record ' + tableSysId);
response.setStatus(200);
response.setBody({
table: tableName,
sys_id: tableSysId,
status: "Success",
message: "File uploaded successfully to record in table " + tableName
});
} else {
// Record not found, respond with failure
gs.error('# Record with Sys ID ' + tableSysId + ' not found.');
response.setStatus(404);
response.setBody({
status: "Failure",
message: "Record not found in table " + tableName
});
}
})(request, response);
Is any modifications required in this script ?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 12:11 AM
I haven't worked on this,
please check these links
Inbound Multipart/form-data via the Scripted REST API (in a scoped application)
Handling 'text/plain' and Other Unsupported Content Types in ServiceNow Scripted REST APIs
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 12:12 AM
what comes in logs for the file data? the bytes etc
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 12:16 AM
