Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Error al decodificador archivos base64 of Salesforce to Jira

Israel Urbina
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 14, 2023

I am trying to create attachments in Jira Cloud using the API, the files are Generated in Jira but the content is not decoded the base64 characters appear. Has anyone had the same error using Salesforce Apex code?

 

// Método para enviar adjuntos.

    @AuraEnabled

 public static String uploadAttachment(String jiraIssueKey, Blob attachmentBlob, String attachmentFileName) {

        String jiraBaseUrl = 'https://org.atlassian.net';

        String jiraAttachmentApiEndpoint = '/rest/api/3/issue/' + jiraIssueKey + '/attachments';

 

            HttpRequest request = new HttpRequest();

            request.setEndpoint(jiraBaseUrl + jiraAttachmentApiEndpoint);

            request.setMethod('POST');

        

            // Autenticación básica

            String username = 'User';

            String accessToken = 'Token';

            String authHeader = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' + accessToken));

        

            request.setHeader('Authorization', authHeader);

            request.setHeader('X-Atlassian-Token', 'no-check');

        

            String boundary = '----------------------------' + Datetime.now().getTime();

            String header = '--' + boundary + '\r\nContent-Disposition: form-data; name="file"; filename="' + attachmentFileName + '"\r\nContent-Type: application/octet-stream\r\n\r\n';

            String footer = '\r\n--' + boundary + '--\r\n';

        

            request.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);

        

            String body = '';

            body += header;

            body += attachmentBlob.toString();

            body += footer;

        

            request.setBodyAsBlob(Blob.valueOf(body));

        

            Http http = new Http();

            HttpResponse response = http.send(request);

        

            if (response.getStatusCode() == 200) {

                return 'Archivo cargado correctamente.';

            } else {

                return 'Error uploading attachment: ' + response.getStatus() + ' - ' + response.getBody();

            }

    }

Comparto Javascript

fileInputHandler(event) {

            const files = event.target.files;

            

            for (let i = 0; i < files.length; i++) {

                const file = files[i];

                const reader = new FileReader();

                reader.onload = (event) => {

                    const attachmentData = event.target.result.split(',')[1]; // Remove the data URI prefix

                    const attachmentFileName = file.name;

 

                    this.uploadAttachmentToJira(attachmentData, attachmentFileName);

                };

                reader.readAsDataURL(file);

            }

        }

 

        async uploadAttachmentToJira(attachmentData, attachmentFileName) {

            try {

                console.log('resultadosFile=', this.Jira_Issue);

                console.log('resultadosFile=', attachmentData);

                console.log('resultadosFile=', attachmentFileName);

                uploadAttachment({ jiraIssueKey: this.Jira_Issue, attachmentBlob: attachmentData, attachmentFileName: attachmentFileName })

                const result = await uploadAttachmentToApex(attachmentData, attachmentFileName);

                console.log(result); 

            } catch (error) {

                console.error(error); 

            }

        }

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events