Sending a POST request with Flow Designer Action

susmita2
Kilo Contributor

Hello, 

Sending a POST request(which has image upload)  through Action in Flow Designer, what are the steps to upload images for POST request in Action and to test? Is there any way to do it? Any reference or videos are helpful.

Thanks in advance.

2 REPLIES 2

Hitoshi Ozawa
Giga Sage
Giga Sage

A POST will upload the file on the server that the script is running to ServiceNow's sys_attachment table.

Flow Designer runs on a ServiceNow server. So, doing a POST using Flow Designer will imply the image to be posted is already on ServiceNow server. If so, it would be faster to just copy the file instead of using a POST API.

To POST an image file that resides on the local computer to ServiceNow, it's necessary to execute the POST script on the local computer instead from Flow Designer.

Hitoshi Ozawa
Giga Sage
Giga Sage

My bad on the previous answer. Flow Designer can be used to send attachment from one instance to another.

Instead of REST step, I usually just use Script step. Just need to page code generated by REST API Explorer.

Example:

find_real_file.png

 

Script:

var table_name = inputs.from_table_name;
var table_sys_id = inputs.from_table_sys_id;
  
var instanceName = '<to_instance_name>';
var user = '<to_user_name>';
var password = '<to_password>';
var insertTableName = inputs.to_table_name;
var insertTableSysId = inputs.to_table_sys_id;

copyAttachments();

function copyAttachments() {
    var att = new GlideRecord('sys_attachment');
    att.addQuery('table_name', table_name);
    att.addQuery('table_sys_id', table_sys_id);
    att.query();
    while (att.next()) {
        gs.info(att.sys_id);
        postAttachment(att);
    }
}

function postAttachment(att) {
        var request = new sn_ws.RESTMessageV2();
        request.setHttpMethod('post');
        request.setEndpoint("https://" + instanceName + ".service-now.com/api/now/attachment/file");
        request.setQueryParameter("table_name", insertTableName); // attach to table name
        request.setQueryParameter("table_sys_id", insertTableSysId); // attach to table record's sys_id
        request.setQueryParameter("file_name", att.file_name);
        request.setRequestHeader("Content-Type", att.content_type);
        request.setRequestHeader("Accept", "application/json");
        request.setRequestBodyFromAttachment(att.sys_id); // file to attach

        request.setBasicAuth(user, password);
        request.setRequestHeader("Accept", "application/json");

        var response = request.execute();
        var httpResponseStatus = response.getStatusCode();

        gs.info("http response status_code: " + httpResponseStatus);
}