inbound attachments via rest

Jack62
Giga Guru

Morning all

 

Now we have attachments sending through a rest integration and attaching to our corresponding ticket on the other side I am having a similar issue with attachments being sent to us. our script is below, we pull this data and currently if the pull shows information for an attachment, I then have to separately pull in the attachment and attach it to the relevant record. The original pull is working fine, but when I try to make the second REST call to save the attachment our end, all I’m getting is a statuscode of 0. I’m assuming I’m missing something obvious here but it would be good if someone can point me in the right direction?

 

This is a screenshot of what we are getting in our pull:

 

Jack62_0-1711272869996.png

 

 

FYI the ‘attachmentInfo’ is the URL of where the attachment is, ‘attachment’ is the url with auth token info etc. using aws.

 

 

Here is the relevant section of coding I am trying to use to pull the attachment into our system:

 

//grab the sysid of the record that we want to attach to:

var findRec = new GlideRecord(table);

findRec.addQuery('correlation_id',p23);

findRec.query();

if(findRec.next()){

var recordSysId = findRec.sys_id;

}

 

var filename = p33;

gs.log('filename ' + p33);

gs.log('table ' + table);

gs.log('recordSysId ' + recordSysId);

//Logs above all display correct info

var ss = new sn_ws.RESTMessageV2('XXXXX integration', 'Get attachment');

ss.setEndpoint(attachment);//attachment var here is the aws url

//ss.setRequestHeader('Content-Type','image/jpeg'); Tried setting this in case that helps, but also tried without

ss.saveResponseBodyAsAttachment(table,recordSysId, filename);

var responseSS = ss.execute();

 httpResponseStatus = ss.getStatusCode();

 var attbody = ss.getResponseBody();

var httpResponseContentType = responseSS.getHeader('Content-Type');

gs.log("http response status_code: " + httpResponseStatus); //This comes back as 0

gs.log("http response content-type: " + httpResponseContentType); //This comes back as application/xml

gs.log('att resp body: ' + attbody); //This is empty

 

 

2 REPLIES 2

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Jack62 ,

 

try this way : sample

SohailKhilji_4-1711274739317.png

SohailKhilji_1-1711274564915.png

 

 

 

I hope this helps...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Amit Pandey
Kilo Sage

Hi @Jack62 

 

The status code of 0 typically indicates that the request did not reach the server or the server did not send back a response. This could be due to various reasons such as incorrect endpoint configuration, or issues with the request itself. Have you tried this with Postman? Also, share the logs received from the below code with me-

 

    var table = 'your_table_name';
    var p23 = current.correlation_id; // Assuming correlation_id is a field on your table
    var filename = 'attachment_file_name.ext'; 

    var findRec = new GlideRecord(table);
    findRec.addQuery('correlation_id', p23);
    findRec.query();

    if (findRec.next()) {
        var recordSysId = findRec.sys_id;
        var attachmentMessage = new sn_ws.RESTMessageV2('XXXXX integration', 'Get attachment');
        attachmentMessage.setEndpoint(attachment); // 'attachment' variable contains the URL with auth token
       // attachmentMessage.setRequestHeader('Content-Type', 'image/jpeg'); // Example header
        
        var attachmentResponse = attachmentMessage.execute();
        var attachmentStatusCode = attachmentMessage.getStatusCode();
        var attachmentBody = attachmentMessage.getResponseText();

        if (attachmentStatusCode == 200) {
 
            current.addAttachment(attachmentBody, filename);
            gs.log('Attachment saved successfully to record: ' + recordSysId);
        } else {
            
            gs.log('Error retrieving attachment: HTTP status code ' + attachmentStatusCode);
        }
    } else {
       
        gs.log('No record found with correlation_id: ' + p23);
    }

Regards,

Amit

 

Regards,

Amit