How do I change proxy request URL using javascript policy ?

Not applicable

Hi All,

I am creating a proxy example where I use APIKEY as authorization method.

I am not passing any key in the URL, instead I am setting key value in environment.

Now I am using a java script policy to get the key value from the environment and try to rewrite URL as below

----------------------------------------------------------------------------------------------------------------------

print("In proxy request flow");
var incoming_request_url = context.proxyRequest.url;
var api_key = context.getVariable("accesskey");
var source_host_path = "http://rssoftwarestaging-test.apigee.net";
print("Incoming Request=="+incoming_request_url);
print("Access Key Value set in environment=="+api_key);
var request_url = source_host_path+incoming_request_url+"?apikey="+api_key;
print("request_url==" + request_url);

context.setVariable("context.proxyRequest.url", request_url);

-------------------------------------------------------------------------------------------------------------------------

After that I am using 'VerifyAPIKey' policy to validate the key that I have set in the environment.

I found that the policy failed because the 'apikey' value is not set in the proxy request.

Can someone please provide me some example to resolve the issue ?

Thanks,

Suvojit

0 4 734
4 REPLIES 4

Not applicable

@Suvojit Chandra,

I feel the issue lies in getting the parameter api_key from contest variable "accesskey". Have you checked out how the accesskey parameter is getting populated in the context ?

Not applicable

Yes I checked it, accesskey value is populated through environment configuration.

I have created a policy KeyValueMapOperations and set the value of access key in the environment variable.

Policy looks like

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="access_key_Target" mapIdentifier="Hello_NodeJs_key">
<DisplayName>access_key_Target</DisplayName>
<Properties/>
<ExclusiveCache>false</ExclusiveCache>
<ExpiryTimeInSecs>-1</ExpiryTimeInSecs>
<Get assignTo="accesskey">
<Key>
<Parameter>key1</Parameter>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>

------------------------------------------------------------------------------------------------------------------------------

key1IJ0KZYHUH0NeSZzr4hVG8BAl8joG7Oxt

Even I print the value of the URL after setting proxyRequest.url in trace and found correct URL

context.setVariable("context.proxyRequest.url", request_url);

context.proxyRequest.url http://rssoftwarestaging-test.apigee.net/v2/hello_nodejs?apikey=IJ0KZYHUH0NeSZzr4hVG8BAl8joG7Oxt

It is correctly set. But the error I received given below

error Failed to resolve API Key variable request.queryparam.apikey
type ErrorPoint
state PROXY_REQ_FLOW
error.class com.apigee.oauth.v2.OauthAdaptorVerificationException
Identifier fault

The point over is that if I run the above mentioned URL(http://rssoftwarestaging-test.apigee.net/v2/hello_nodejs?apikey=IJ0KZYHUH0NeSZzr4hVG8BAl8joG7Oxt) through Postman I can get successful response.

Please help me that I if I am doing some wrong over here or not.

Not applicable

Can you please also provide your VerifyAPIKey policy ? Also, would it be possible for you to attach the code here ?

Not applicable

Sure,

Please find 'VerifyAPIKey' Policy below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VerifyAPIKey async="false" continueOnError="false" enabled="true" name="verify-api-key">
<DisplayName>Verify API Key</DisplayName>
<APIKey ref="request.queryparam.apikey"/>
</VerifyAPIKey>

please find complete code below (attached proxy).

hello-nodejs-rev2-2016-08-02.zip