Can anyone help me with this code - Use of GlideSysAttachmentInputStream in this GET Scripted API.

SamritiGrover
Tera Contributor
 
When I am checking in REST API Explorer/Postman, it's giving response in binary.
 
 
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var attachmentid = request.pathParams.attachmentId;
    var caseid = request.pathParams.id;
    var writer = response.getStreamWriter();
    response.setContentType('application/json');
    var body = {};
    var caseGr = new GlideRecord('Custom Table Name');
    caseGr.addQuery('number', caseid);
    caseGr.query();
    if (caseGr.next()) {
        var attachGr = new GlideRecord('sys_attachment');
        attachGr.addQuery('sys_id', attachmentid);
        attachGr.addQuery('table_sys_id', caseGr.sys_id);
        attachGr.query();
        if (attachGr.next()) {
            var attachmentStream = new GlideSysAttachmentInputStream(attachmentid);
            response.setHeader('x-swo-file-name', attachGr.file_name.toString());
            response.setHeader('x-swo-file-content-type', attachGr.content_type.toString());
            response.setHeader('x-swo-file-sizeInBytes', attachGr.size_bytes.toString());
        }
        response.setStatus(200);
        writer.writeStream(attachmentStream);

    } else {
        response.setStatus(404);
        response.setContentType('application/json');
        var result = {
            "title": "Not Found",
            "status": 404
        };
        writer.writeString(JSON.stringify(result));
    }
    // implement resource here

})(request, response);
0 REPLIES 0