Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to attach file same as OOB table api in postman from scripted web services ?

Thej1
Tera Expert

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.

image (7).png

 

But for the custom scripted web services, we are send request in JSON body as below but not in multipart/form-data.

{
  "incidentNumber""INC0010002",
  "fileName""Testtt.png",
    "contentType""image/png",
  "fileData": "Base64 code"
}
Is there a way to do it to send request in form-data same as of the OOB table api for the custom scripted web service ?
 
Thanks
1 ACCEPTED SOLUTION

@Thej1 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

@Thej1 

Please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.

Thej1_0-1737532877488.png

 

But getting error response as below

{
    "result": {
        "status""Failure",
        "message""No file was uploaded."
    }
}
 
The script is 

 

 

(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

 

@Thej1 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Thej1 

what comes in logs for the file data? the bytes etc

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
The logs are as below SS not showing the file data.

Thej1_0-1737533756054.png