cancel
Showing results for 
Search instead for 
Did you mean: 

Get Binary Data from REST API response

Waldy
Deputy Chef III
Deputy Chef III

Hi there,

             I'm getting an error from the get method in my connector when I try to get the response of a REST API method that returns binary data.

The error is 783: unexpected token at '����' at line:

I'm guessing it is trying to parse the response as JSON.

The code is:

 

execute: lambda do |connection, input|
        # Fetch attachment binary data
        response = get("#{connection['tenant_url']}/api/rest/attachment?ID=#{input['RecId']}").
          response_format_raw. # Treat response as raw binary data
          after_error_response(/.*/) do |_code, body, _header, message|
            error("#{message}: #{body}")
          end
1 ACCEPTED SOLUTION

Waldy
Deputy Chef III
Deputy Chef III

I got it working like this:

execute: lambda do |connection, input|
        # Fetch attachment binary data
        response = get("#{connection['tenant_url']}/api/rest/attachment?ID=#{input['RecId']}")
          .response_format_raw # Treat the response as raw binary data
          .after_error_response(/.*/) do |_code, body, _header, message|
            error("#{message}: #{body}")
          end
          # Encode binary data to Base64
          {
            RecId: input['RecId'],
            FileData: Base64.strict_encode64(response.to_s)
          }
      end

View solution in original post

4 REPLIES 4

shivakumara
Executive Chef III
Executive Chef III

Hi @Waldy ,
Here I am adding a ruby with small changes with an explanation.

execute: lambda do |connection, input|
  # Fetch attachment binary data
  get("#{connection['tenant_url']}/api/rest/attachment?ID=#{input['RecId']}")
    .response_format_raw # Treat the response as raw binary data
    .after_error_response(/.*/) do |_code, body, _header, message|
      error("#{message}: #{body}")
    end
    .tap do |response|
      {
        binary_content: response.body,
        headers: response.headers
      }
    end
end

 

1. .response_format_raw tells Workato not to parse the response as JSON.
2.  .tap is used to construct a return payload with binary_content and headers so that you can access both.

I hope this will help you, please kindly try it from your end and let me know if you need further help.

Thanks and Regards,
Shivakumara K A

 

Hi @shivakumara , 

 That results in an error:  undefined method `body' for #<String:0x0000563ae74cf888> ...  I'm guessing because it is expecting body to be in the response?

Waldy
Deputy Chef III
Deputy Chef III

I got it working like this:

execute: lambda do |connection, input|
        # Fetch attachment binary data
        response = get("#{connection['tenant_url']}/api/rest/attachment?ID=#{input['RecId']}")
          .response_format_raw # Treat the response as raw binary data
          .after_error_response(/.*/) do |_code, body, _header, message|
            error("#{message}: #{body}")
          end
          # Encode binary data to Base64
          {
            RecId: input['RecId'],
            FileData: Base64.strict_encode64(response.to_s)
          }
      end

shivakumara
Executive Chef III
Executive Chef III

Hi @Waldy ,

Great to hear that! Since I wasn't able to test it on my end, I shared the code from my repository that is working.

Thanks again for keeping me informed!



Thanks and Regards,
Shivakumara K A