How to return a PNG image in the response payload?

Not applicable

I want to return a PNG image in the response payload. I am retrieving the image from an API that I call in a JavaScript file used by a JS Policy in the proxy. I have included the code snippet from JS file below but it is not working (with credentials inserted, of course). The variable "response" stays undefined even after the API call.

var req = new Request("https://tile2.aerisapi.com/credentials/current_temps/500x500/40/-105/10/0.pn",
                      'GET'
                      ); 
var exchange = httpClient.send(req);
exchange.waitForComplete();
var response = exchange.getResponse(); 
var responseObject = response.content;
print(response);
1 8 19.7K
8 REPLIES 8

sarthak
Participant V

Hi @Eskinder Zewdu

This script worked fine for me:

var headers = {'Accept':'image/jpeg'};
var req = new Request("https://api.usergrid.com/pixvy/sandbox/pictures/anubis",
                      'GET',headers); 
var exchange = httpClient.send(req);
exchange.waitForComplete();
if (exchange.isSuccess()) {
    var responseObj = exchange.getResponse().content; 
    context.setVariable("response.content",responseObj);    
}

Invoke it : curl http://pixvy-test.apigee.net/retrieveimages

Let me know if you have further questions or issues.

@sarthak. I am using the avoe script but unable to return the image in response. The response is is coming in some wired way. Please let me know if any other settings we need to do to achieve this.

Hi @Eskinder Zewdu

I just tried this and it works for me, using a specific image server (imgur). Can you please elaborate on what you are trying to do? If you'd like to manipulate or proxy a binary image, you may have a better time of it, if you use nodejs target, and script it there. The reason is, the nodejs script can take advantage of the npm request module, which gives you a lot of power for handling requests, response headers, status, and so on.

@Dino. I am using the avoe script but unable to return the image in response. The response is is coming in some wired way. Please let me know if any other settings we need to do to achieve this

Not applicable

@sarthak & @Dino- although that script will get a response, there are some additional issues:

1) When I use https, I get the SSL error - Received fatal alert: handshake_failure

2) With http, I get a response but it is the raw PNG data.

To clarify - I am making a JS callout in my API proxy to request the image from an external API.

If I make the request in Postman, I get the image rendered properly. I think it may be an encoding issue when making the request via Apigee. Is there a JS workaround used for callouts in Apigee?

Here's my current code -

var headers = {'Accept':'image/png'};

var req =newRequest("https://tile2.aerisapi.com/credentials/current_temps/500x500/40/-105/10/0.png",'GET',headers);
var exchange = httpClient.send(req);
exchange.waitForComplete();
var response = exchange.getResponse(); 

if (exchange.isSuccess()) {
    var responseObject = exchange.getResponse();
    print(responseObject);
    context.setVariable('awhere.imageResponse',responseObject.content);  
}else{
    print(exchange.getError());
}

How can I resolve these issues? Thanks again!

@Dino we are seeing a slightly different set of behavior.

we are straight up proxying a back end that can return many kinds of content, including images.

The problem we are having is that when we go through the gateway w/ a callout we get the image sent back as a download rather than embedded the way we want it. When we call it directly we get the expected behavior...

We are not sure exactly what is causing that.

john_young
Participant II

Agreed with @Eskinder - you get a response but it's corrupted.

Looking with fiddler it appears that the response is being treated as UTF-8 and then non-printing characters are translated to the UTF-8 "replacement character" sequence of "EF BF BD"

So, is it possible to fetch and forward arbitrary binary data with the Apigee httpClient or are we forced to go with node.js?

Not applicable

Hi @Jon Young - I thought my answer had posted. It hadn't, my apologies. Will make sure to include you in the other thread.