Error while decoding attachment in base64

FredericoM
Tera Contributor

Hello,

I am trying to create a Scripted REST Resource to receive attachments for a scoped application. The attachments are sent encoded in base64. It is working correctly when receiving .txt documents, but when receiving other types of attachments like jpeg or pdf, the file is attached but when performing a subsequent download, the file is corrupted.

here is my code:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

	var ticket = request.pathParams.ticketId;
        var body = request.body.data;
	var attachment;
	
	try{
		attachment = body.anexos;

		var gr = new GlideRecord 		
		('x_cdpdf_uai_200_item_de_integracao');
		gr.addQuery ('ticket_externo', ticket);
		gr.query();

		if (!gr.hasNext()){
			throw new Error ('Ticket não localizado.');
		}

		if (gr.next()){

				//Tratar anexos
				attachment.forEach(item =>
					{
						var anexo = new GlideSysAttachment();
						var decodeData = gs.base64Decode(item.arquivo);
						anexo.write(gr, item.nomeArquivo, decodeData);
					});			
				gr.work_notes = "Attachment received";
				gr.update();

				response.setStatus(200);
				response.setBody({
					message: "Attachment received"
					
				});
		}
	} catch(e){	
		response.setStatus(400);
		response.setBody({error: "Erro ao incluir uma conversa." + e.message});
		return;
	}
})(request, response);

 

Any suggestion of what might be wrong?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@FredericoM 

since you said your scripted REST API is in scoped app, you need to use this method writeBas64 which is available for scope app. Don't decode.

GlideSysAttachment - Scoped 

update script as this

Note: You will have to pass file name and content type so that system understands, ask 3rd party to send that if they are not sending it

var anexo = new GlideSysAttachment();
anexo.writeBase64(gr, fileName, contentType, item.arquivo);

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

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@FredericoM 

since you said your scripted REST API is in scoped app, you need to use this method writeBas64 which is available for scope app. Don't decode.

GlideSysAttachment - Scoped 

update script as this

Note: You will have to pass file name and content type so that system understands, ask 3rd party to send that if they are not sending it

var anexo = new GlideSysAttachment();
anexo.writeBase64(gr, fileName, contentType, item.arquivo);

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

Hi Ankur

 

In the end I found out that the problem was that the files I was using for testing were not in binary format. I did the conversion again and got it working with writeBase64 as suggested.

 

Thank you very much.

 

 

@FredericoM 

Glad to help.

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