Unable to get Target url

Not applicable

Hi,

@Sai Saran Vaidyanathan @Dino-at-Google @deboraelkin @Siddharth Barahalikar

I have a node.js application(deployed using Hosted Target) (with query parameter). What I am trying to do is, When I hit my current url I want to redirect it to a new url (my cloud function url). I have used a javascript policy for it. The script is somethinglike:

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

var url = 'https://xyz.com';

context.setVariable("target.url",url);

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

(I have my javascript policy on Targetendpoint Response flow. )

But I am getting an error:

Cannot GET /xyz
0 4 637
4 REPLIES 4

I'm not sure I understand what you're trying to do.

Do you want your proxy to route to your Hosted Target app? If that's the case, you'll need to add a

<HostedTarget/>

element to your TargetEndpoint.

Are you trying to get your hosted target app to talk to some other target URL? Then use a Node.js package such as http or request.

Do you need to invoke a URL after the Hosted Target app has returned its response (ie: in the target endpoint response flow)? If so, use a service callout in that flow

Finally, if you still want to run the javascript code snippet you've included in the target endpoint response flow, bear in mind that the target url has already been invoked by then, so there' no point in trying to change it.

If you really, really want to change it (probably not necessary, see other possible solutions above), you'd need to include it in the target endpoint request flow and use it, not with a hosted target, but with a regular target endpoint

"Do you need to invoke a URL after the Hosted Target app has returned its response (ie: in the target endpoint response flow)? If so, use a service callout in that flow"

deboraelkin Could you help me with how to use service callout in Java Script Policy, what are the codes so that I can redirect to a new url when ever I hit my old one.

I still don't understand what you're trying to achieve. Where do you want to redirect to? And when? Before, during or after executing your Hosted Target app?

Also, a service callout is an out of the box policy (See https://docs.apigee.com/api-platform/develop/policy-attachment-and-enforcement and https://docs.apigee.com/api-platform/reference/policies/service-callout-policy). It can't be embedded within Javascript code

Hi Yase

I can't see how Hosted Target is relevant to what you're trying to do.

With that said, if you're trying to trigger a redirect to occur from your Apigee proxy eg your client is a web browser, that you want to go to your apigee proxy then be redirected then this could be done by setting one of the HTTP Redirect Status codes (eg 301, 302..) and a location header with the redirect url.

Here's an example implementation using the AssignMessage policy which is setting a status code of 302 for a temporary redirect and using google.com as the redirect url.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Set>
        <StatusCode>302</StatusCode>
        <Headers>
            <Header name="Location">https://google.com</Header>
        </Headers>
        <Path/>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Going back to what you've tried to do.. The target url you are setting in your javascript policy is setting the target endpoint of your Apigee proxy. This is used when you're using Apigee as a reverse proxy - Apigee makes a request to the specific target url and will return the response to the consumer of the proxy. This is very different from a redirect. Also, you can either use a hosted target or a reverse proxy.. When using a hosted target, the hosted target becomes your target endpoint.

ie redirect approach

Browser (GET) -> Apigee Proxy
Browser <- (Redirect status, location header url) Apigee Proxy
Browser (GET) -> Redirect location URL
Browser <- Redirect content

ie reverse proxy with target.url

Browser (GET) -> Apigee Proxy
                 Apigee (GET) -> target.url
                 Apigee <- target content
Browser <- (target content) Apigee proxy