How to send a binary representation of a base64 string using Postman?

160 Views Asked by At

I have a response containing a base64 string that I need to convert to binary and send to the Bytescale API to generate a PDF file in POSTMAN. My test runs without any errors, but the resulting PDF file is blank. What could be causing this issue?

My response:

[
{
        "itemId": "3286457",
        "label": "JVBERi0xLjQKJfbk/.....................",
        "contentType": "application/pdf"
    }
]

My Test Script in POSTMAN:

// Base64 encoded PDF string from the response
const responseArray = pm.response.json();
const base64String = responseArray[0].label; 

// binary data 
var binaryData = atob(base64String);

// POST request
pm.sendRequest({
    url: 'https://api.bytescale.com/v2/accounts/xxxxxxx/uploads/binary',
    method: 'POST',
    header: {
        'Accept': 'application/pdf',
        'Content-Type': 'application/pdf',
        'Authorization': 'Bearer public_xxxxxxx'
      },
    body: binaryData,
}, (error, response) => {
    if (error) {
        console.error('Request failed:', error);
    } else {
        console.log('Response status code:', response.code);
        console.log('Response body:', response.text());
        
        if (response.code === 200) {
            const pdfLink = response.json().fileUrl;
            console.log('PDF Link:', pdfLink);
        } else {
            console.error('Request was not successful. Response code:', response.code);
        }
    }
});

I want to use this API https://www.bytescale.com/docs/upload-api/BasicUpload

0

There are 0 best solutions below