Sending Attachment Image from ServiceNow to Postman

dsaha
Tera Contributor

Hello,

 

I am trying to do a GET request from Postman to ServiceNow to pull the latest image which gets attached in a ticket.

I have written the below code in Scripted Rest API to generate the endpoint.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    try {
        // Extract query parameters
        var queryParams = request.queryParams;
        gs.info('Received queryParams: ' + JSON.stringify(queryParams)); // Log all query parameters

        var taskSysId = queryParams.task_sys_id;  // Extract task_sys_id
        var attachmentSysId = queryParams.attachment_sys_id;  // Extract attachment_sys_id

        // Log the received parameters for debugging
        gs.info('Received task_sys_id: ' + taskSysId);
        gs.info('Received attachment_sys_id: ' + attachmentSysId);

        // Validate task_sys_id
        if (!taskSysId) {
            response.setStatus(400);
            response.setBody({ error: "Missing task_sys_id parameter" });
            return;
        }

        // If attachment_sys_id is provided, retrieve that specific attachment
        if (attachmentSysId) {
            var attachmentGr = new GlideRecord('sys_attachment');
            gs.info('Querying for attachment with sys_id: ' + attachmentSysId);

            // Check if the attachment exists
            if (!attachmentGr.get(attachmentSysId)) {
                gs.info('Attachment not found for sys_id: ' + attachmentSysId); // Log if not found
                response.setStatus(404);
                response.setBody({ error: "Attachment not found" });
                return;
            }

            // Get attachment details
            var fileName = attachmentGr.getValue('file_name');
            var contentType = attachmentGr.getValue('content_type');

            // Retrieve the file data
            var attachment = new GlideSysAttachment();
            var fileData = attachment.getBytes(attachmentGr);

            // Set response headers
            response.setHeader('Content-Type', contentType);
            response.setHeader('Content-Disposition', 'attachment; filename="' + fileName + '"');

            // Send the attachment data as the response body
            response.setStatus(200);
            response.setBody(fileData);

        } else {
            // If no specific attachment_sys_id is provided, return all attachments for the task
            var attachmentList = [];
            var attachmentGr = new GlideRecord('sys_attachment');
            gs.info('Querying for attachments for task_sys_id: ' + taskSysId);

            // Query attachments for the given task_sys_id
            attachmentGr.addQuery('table_sys_id', taskSysId);
            attachmentGr.query();

            while (attachmentGr.next()) {
                attachmentList.push({
                    sys_id: attachmentGr.getValue('sys_id'),
                    file_name: attachmentGr.getValue('file_name'),
                    content_type: attachmentGr.getValue('content_type')
                });
            }

            if (attachmentList.length === 0) {
                response.setStatus(404);
                response.setBody({ error: "No attachments found for task_sys_id: " + taskSysId });
                return;
            }

            response.setStatus(200);
            response.setBody({ attachments: attachmentList });
        }

    } catch (ex) {
        gs.error('Error processing request: ' + ex.message);
        response.setStatus(500);
        response.setBody({ error: ex.message });
    }
})(request, response);

 

However I need to get the base 64 content as well. But I am not receiving the same in the response body.

 

The response I am getting is below


{
  "result": {
    "attachments": [
      {
        "sys_id": "e006b1fceb049a109ee8f99bcad0cd30",
        "file_name": "issue_dipanjan123 (9).jpg",
        "content_type": "image/jpeg"
      }
    ]
  }
}

 

Can someone help me to fix the issue?

Thanks in advance.

 

Regards,

Dipanjan Saha

0 REPLIES 0