Decode Base64 Async Responses in NodeJS

Learn to decode Base64-encoded binary responses from ScraperAPI Async in NodeJS. Process PDFs, images, and files with NodeJS and command-line examples.

The responses returned by the Async API for binary requests require you to decode the data as they are encoded using Base64 encoding. This allows the binary data to be sent as a text string, which can then be decoded back into its original form when you want to use it.

Example request:

const axios = require('axios');

(async () => {
const { data } = await axios.post('https://async.scraperapi.com/jobs', {
apiKey: 'API_KEY',
url: 'https://pdfobject.com/pdf/sample.pdf'
}, {
headers: { 'Content-Type': 'application/json' }
});

console.log(data);
})();

Decode response:

const axios = require('axios');
const fs = require('fs');

(async () => {
const response = await axios.get('https://async.scraperapi.com/jobs/<JOB_ID>');
const base64EncodedBody = response.data.response.base64EncodedBody;
const pdfData = Buffer.from(base64EncodedBody, 'base64');
  
fs.writeFileSync('name.pdf', pdfData);
})();

Last updated

Was this helpful?