Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

HTTP Client get request issue

Not applicable

Hi @Mike Dunker @Michael Russo,

I am currently going through the Edge Developer bootcamp, doing exercise 2 in advance proxy development where I am using a javascipt to call out to an API. I don't seem to be getting response from the API, I have tried debugging by printing out some info throughout the javascript, I have posted below the callout javascript, assemble response and the message i am getting in the transactions output when the first javascript is failing.

JavaScript 1

print("One");
var headers = {'Authorization':'Basic dHJhaW5pbmc6bXlwYXNz'}; 
var url = "http://jokeindex.com/services/20/api/jokes"
var myRequest = new Request(url,"GET",headers);
print("two" + headers);
var exchange = httpClient.send(myRequest);
exchange.waitForComplete();
print("three" + exchange);

// AssembleResponse
function dataResponse(data){
  var message = data;
  print("Final Message " + message);
  return message;
}


Output from error

One

two[object Object]

Could you please help me identify what I am doing wrong.

Solved Solved
0 12 3,509
1 ACCEPTED SOLUTION

Hi Jonny,

can you please try executing below code. I guess the part that is missing is retrieving the response using proper method over exchange object.

getdata.js -(credentials removed in the header part)

print("--------------------Get Data Start-------------------");
var headers = {'Authorization':'Basic XXXXXXXXXXX'};
var url = "http://jokeindex.com/services/20/api/joke?jokeid=3229";
var myRequest = new Request(url,"GET",headers);
print("Request object created");
var exchange = httpClient.send(myRequest);
context.session['exchange'] = exchange;
print("Request sent");
print("--------------------Get Data End-------------------");

assembleResponse.js

print("--------------------Assemble Response Start-------------------");
var exchange = context.session['exchange'];
exchange.waitForComplete(1000);
print("Is Sent successfully in Assemble response --> " + exchange.isSuccess());
print("Response Content --> " + exchange.getResponse().content);
//Assign to Apigee response variable
context.setVariable("response.content",exchange.getResponse().content);
print("--------------------Assemble Response End-------------------");

Please see if it works.

Regards,

Santosh

View solution in original post

12 REPLIES 12