How to handle 301 Moved Permanently (Redirect URL in apigee from target server)

How do I need to handle if a 301 Redirect URL occurs from target server

I want to redirect to redirect URL from apigee when target server has a redirect URL.

Redirection is handled automatically in Rest clients.

Can the same be done from apigee instead of clients handling the redirect.

2941-capture.jpg

Solved Solved
1 16 6,775
1 ACCEPTED SOLUTION

@Atanu Mazumdar , I am missing something here, Client -> Target -> Target Response -> 301 Response Header -> Client -> Clinet does Redirect.

What you are saying is ,

Client -> Target -> Target Response -> 301 Response Header -> Make an API call to the redirect URL -> Get Response -> Send to client ? Is it what you are expecting ?

It should be simple, Just execute a service callout on condition if target response is 301 & grab the url from response headers. And then send response from service callout to client. It's called mashup & it's available out of the box. Refer here for flow variables, Refer here for conditional execution of policy, Refer here for service callout.

View solution in original post

16 REPLIES 16

@Atanu Mazumdar

I assume you want to send a custom message.for that error.

In Target end point flow you can give like below

<HTTPTargetConnection> <Properties> <Property name="success.codes">1xx,2xx,3xx,4xx</Property> </Properties> <URL>https://api.usergrid.com</URL> </HTTPTargetConnection>

and it goes through the normal flow. Now you can handle by adding a policy in the response flow.

There is a redirect url in Response Headers .Location

How to redirect to this Location URL

Dear @Atanu Mazumdar,

You need to set the "target.header.host" with the appropriate host name either using Assign Message Policy or in a Javascript code.

Here's a sample Assign Message policy showing how to do this:

<AssignMessage continueOnError="false" async="false" name="AddHostHeader">
    <AssignTo createNew="false" type="request"></AssignTo>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignVariable>
        <Name>target.header.host</Name>
        <Value>www.somehost.com</Value>
    </AssignVariable>
</AssignMessage>

Or you can use Javascript to do the same as follows:

context.setVariable("target.header.host", "www.somehost.com");

Hope that helps.

Regards,

Amar

@Atanu Mazumdar

What do you mean by handle ? Is target response giving 301 ? What would you like to do in Apigee ? What is the expected outcome ? I believe, your question is not complete. Please update your question with more details.

Redirect to where ? Apigee Edge Proxy just returns the same to the client. Client has to handle the redirect. Just think like your target is giving 301 , What would you like to do in Apigee ?

@Atanu Mazumdar

Apigee is just a target for your client view. Client has to handle redirects, Redirect in Apigee doesn't make any sense. You need to send the response back to the client. Apologies, If i am missing something here.

If you mean, Making an API call to redirect URL from the target response, you can leverage Service Callouts. Ultimately, You need to send response back to client right ? What do you mean exactly when you said "Redirect in Apigee" ?

I agree Redirects are done from REST clients. .. but if I am getting the redirect URL in Response Headers .Location of apigee ..... then why can't it be done from apigee itself instead of clients doing the same.

Problem here is for my proxy I am sending a OAuth access token as 'Authorization' header.

But my target URL also requires a 'Authorization' header which I am setting it internally as 'Assign Message' policy.

Now we cant send two different 'Authorization' header from rest client if you want rest client to handle the redirect.

@Atanu Mazumdar , Remove the Authorization header in Apigee Proxy flow after verifying access token using Assign Message Policy & then add target url related authorization header. Does that help ?

I am already adding target URL related authorization header in Assign Message.

Authorization header is getting set but how to do the redirect from target server(means again reset the target url which we are getting as part of Response Headers .Location).

@Atanu Mazumdar , I am missing something here, Client -> Target -> Target Response -> 301 Response Header -> Client -> Clinet does Redirect.

What you are saying is ,

Client -> Target -> Target Response -> 301 Response Header -> Make an API call to the redirect URL -> Get Response -> Send to client ? Is it what you are expecting ?

Yes exactly... make an api call only when we get 301 Response Header and the redirect url in your Response Headers .Location.

Do what REST clients does for you instead.

@Atanu Mazumdar , It should be simple, Just execute a service callout on condition if target response is 301 & grab the url from response headers. And then send response from service callout to client. It's called mashup & it's available out of the box. Refer here for flow variables, Refer here for conditional execution of policy, Refer here for service callout.

This might help ... thanks

Glad to know, Posted as an answer too, Feel free to accept Answer if your query is resolved so that it will be helpful for others too. Thank you.

@Atanu Mazumdar , I am missing something here, Client -> Target -> Target Response -> 301 Response Header -> Client -> Clinet does Redirect.

What you are saying is ,

Client -> Target -> Target Response -> 301 Response Header -> Make an API call to the redirect URL -> Get Response -> Send to client ? Is it what you are expecting ?

It should be simple, Just execute a service callout on condition if target response is 301 & grab the url from response headers. And then send response from service callout to client. It's called mashup & it's available out of the box. Refer here for flow variables, Refer here for conditional execution of policy, Refer here for service callout.

This solution works but :-

1. Be ready to get a performance hit as its not an actual redirect. We are checking target server response and again making an api call.

2.Firewall ports if not opened between your redirect url and apigee may again cause a problem.

@Atanu Mazumdar , Agree with you , Valuable & Valid points. Performance hit is minuscule, few ms while checking the condition, few more ms if we are making one more API call.