3 weeks ago - last edited 3 weeks ago
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:
Solved! Go to Solution.
3 weeks ago
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
3 weeks ago
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
3 weeks ago
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?
3 weeks ago
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
3 weeks ago
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