Need to append apigee host in Response using java script

Hi All,

I would like to access apigee host name in java script in order to set it in the response, so I would like to fetch the value in java script file in response flow of apigee like below.

context.getVariable("request.header.host") // this statement giving me Target URL host.

Any suggestions would be appreciated.

Solved Solved
0 4 257
1 ACCEPTED SOLUTION

jaupadhyay
Participant IV

Hi @Hari Kumar

Alternatively you can try this virtualhost context variable to fetch the information.

// virtualhost.aliases will return all the virtual hosts 
//from the environment where proxy has been deployed
// If proxy is deployed only in one environment  
//it will return array with single element.
// You can set  virtual host alias correctly to get
// expected result
// baseurl variable will have value you need

var virtualhosts = context.getVariable("virtualhost.aliases");
var baseurl = "https://"+virtualhosts[0];

// Check the value for baseurl
print("baseurl "+baseurl);

see if above solution is appropriate with what you are trying to achieve.

View solution in original post

4 REPLIES 4

sujnana
Participant IV

jaupadhyay
Participant IV

Hi @Hari Kumar

Alternatively you can try this virtualhost context variable to fetch the information.

// virtualhost.aliases will return all the virtual hosts 
//from the environment where proxy has been deployed
// If proxy is deployed only in one environment  
//it will return array with single element.
// You can set  virtual host alias correctly to get
// expected result
// baseurl variable will have value you need

var virtualhosts = context.getVariable("virtualhost.aliases");
var baseurl = "https://"+virtualhosts[0];

// Check the value for baseurl
print("baseurl "+baseurl);

see if above solution is appropriate with what you are trying to achieve.

Thanks Jayesh, it worked exactly!

Not applicable

The hostname of apigee is present in proxy endpoint request flow. So, to access that in the response flow, first, you need to store the "request.header.host " to another variable using assign message or javascript in the proxy endpoint preflow request and then use that in the response flow in target endpoint