Decoding

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:

require 'net/http'
require 'json'

uri = URI('https://async.scraperapi.com/jobs')

response = Net::HTTP.post(uri, {
"apiKey" => "API_KEY",
"url" => "https://pdfobject.com/pdf/sample.pdf"
}.to_json, "Content-Type" => "application/json")

print(response.body)

Decode response:

require 'net/http'
require 'json'
require 'base64'

uri = URI('https://async.scraperapi.com/jobs/<JOB_ID>')

response = Net::HTTP.get(uri)
json_response = JSON.parse(response)
base64_encoded_body = json_response['response']['base64EncodedBody']
pdf_data = Base64.decode64(base64_encoded_body)

File.open('name.pdf', 'wb') { |file| file.write(pdf_data) }

Last updated

Was this helpful?