how to use variables in target endpoint url

Not applicable

Hi,

As per our use case we have to use variables in url which changes for every request. I am using following config in target endpoint;

 <HTTPTargetConnection>
   <Properties/>        
   <URL>https://{hostname}/abc/{OID}/xyz?query={pqr}</URL>
 </HTTPTargetConnection>

OID and pqr will be changing for each transaction. The flow fails when Proxy endpoint passes control to the target endpoint. In trace, at "target endpoint started", I see, target URL appears as " https://{hostname}/abc/{OId}/xyz?query={pqr}", So variable values are not getting placed in URL, any guess on solution?

0 11 10K
11 REPLIES 11

Not applicable

For info, I am using java script in previous policies to set these variables.

Not applicable

Hello @ansmayank,

Can you please something like this:

1. In the javascript, set the variable target.url as follows (and place this js before hitting the target endpoint:

// After the values of parameters retrieved

var targetURL = 'https://' + hostname + '/abc/' + OID + '/xyz?query=' + pqr;
context.setVariable("target.url",targetURL);

2. In the target default.xm, configure a default value of target (which may not be getting used):

  <HTTPTargetConnection>
        <URL>https://will-be-replaced.com</URL>
  </HTTPTargetConnection>

Hope this helps..!

And remember that the policy that sets target.url must run in the Target Request flow!

Not in the Proxy request flow!

This is great. But, what if I have a target server in an environmental configuration? dev/test/prod ?

ask a new question please.

Not applicable

You can use javaScript policy or AssignMessage policy to set the target URL and use the variable in TargetConnection.

you can refer the detailed information here to set variable target.url

And remember that the policy that sets target.url must run in the Target Request flow!

Not in the Proxy request flow! If you attach it to the proxy request flow, it will not fail, but it will be ineffective. The target.url gets reset at the inception of the target request flow.

Hi Dino,

I had this issue and I have also ensured the script setting target.url is in "Target Request flow", but is still getting this error. below link contains my information. Thanks

https://community.apigee.com/questions/21143/getting-error-protocolhttpemptypath-when-trying-to.html...

Having set the target.url using the JS setVariable (in the target preflow) I receive the following error:

{    
  "fault": {"faultstring": "Request path cannot be empty",
  "detail": { "errorcode": "protocol.http.EmptyPath"

Could someone explain why this is happening?

Thanks in adv,

Michael McD.

It seems that wrapping the context.setVariable(.... in an

if(context.flow=="TARGET_REQ_FLOW")...

prevents the fault mentioned above being thrown.

However I still don't understand this behaviour and would appreciate any thoughts on this.

Hi

Please avoid asking new questions in "Answers" posted to existing questions.

If you want to ask a new question, click the "Ask a Question" button.

4404-ask-a-question-click-here.png

When you do ask, you will need to provide more than 1/2 a line of JS code.

We'll need more context. If you want to reference THIS question, you can do so with this link;

https://community.apigee.com/questions/33881/how-to-use-variables-in-target-endpoint-url.html