Trying to redirect to the new url from the target server in apigee edge?

How can we redirect to the new url in Apigee Edge from the target server?

What are the policies are required to configure this scenario? I already configured by using Authorization code grant type by giving the call back url in application but now I am trying to configure without using the Authorization code grant type.

Solved Solved
3 8 7,838
1 ACCEPTED SOLUTION

To issue a redirect from Apigee Edge, you can use the AssignMessage policy. Within the policy, set the Location header, and set the StatusCode to be 302 or 301 (as you prefer). You probably should remove the Payload, if there is one. You may also want to set CORS headers. This policy should run on the response flow.

For example:

<AssignMessage name="AM-Redirect">
  <DisplayName>Assign Headers for 302 Redirect</DisplayName>
  <Remove>
    <Payload/>
  </Remove>
  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">http://the-new-location-here/etc</Header>
      <Header name="Access-Control-Allow-Origin">*</Header>
      <Header name='Access-Control-Allow-Methods'>POST, GET, OPTIONS</Header>
      <Header name='Access-Control-Allow-Headers'>Accept, Content-Type, connection, content-length, Authorization, Location</Header>
      <Header name='Access-Control-Allow-Credentials'>true</Header>
    </Headers>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

If the redirect location is dynamic, in other words if it is determined by the value of a context variable, then you can just surround the variable name in curlies, like this:

  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">{variable_containing_url}</Header>
       ...

You can also compose a URL from multiple variables, like this:

  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">{base_redirect_url}/path?q1={query_param_value}</Header>
       ...

Good luck!

View solution in original post

8 REPLIES 8

To issue a redirect from Apigee Edge, you can use the AssignMessage policy. Within the policy, set the Location header, and set the StatusCode to be 302 or 301 (as you prefer). You probably should remove the Payload, if there is one. You may also want to set CORS headers. This policy should run on the response flow.

For example:

<AssignMessage name="AM-Redirect">
  <DisplayName>Assign Headers for 302 Redirect</DisplayName>
  <Remove>
    <Payload/>
  </Remove>
  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">http://the-new-location-here/etc</Header>
      <Header name="Access-Control-Allow-Origin">*</Header>
      <Header name='Access-Control-Allow-Methods'>POST, GET, OPTIONS</Header>
      <Header name='Access-Control-Allow-Headers'>Accept, Content-Type, connection, content-length, Authorization, Location</Header>
      <Header name='Access-Control-Allow-Credentials'>true</Header>
    </Headers>
  </Set>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

If the redirect location is dynamic, in other words if it is determined by the value of a context variable, then you can just surround the variable name in curlies, like this:

  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">{variable_containing_url}</Header>
       ...

You can also compose a URL from multiple variables, like this:

  <Set>
    <StatusCode>302</StatusCode>
    <Headers>
      <Header name="Location">{base_redirect_url}/path?q1={query_param_value}</Header>
       ...

Good luck!

Yes, I implemented what you mentioned above and get the response from the redirect url . But the

requirement is to specify the assign message policy in request proxy flow(is it possible).

No - To assign a value to the response, you must use AssignMessage in the response flow. If necessary, you can set a flag in the request flow, and then use a condition that tests that flag, to wrap the AssignMessage policy in the response flow.

If possible can you please specify some example for setting a flag in the request flow

Feeling very glad because you mentioned the answer very clear. Thank you very much.

I'm trying to redirect ( forward ) the response from target endpoint.

The Redirect is done successfully however, however the payload returned is not included ? what should I do to forward the response payload to the redirect URI location ?

I'm not clear what you mean by

I'm trying to redirect ( forward ) the response from target endpoint.

The Apigee Edge API Proxy is a reverse proxy. It automatically returns the response obtained from the target, to the originating client. There's nothing special you need to do, to make that happen. If you like you can configure APigee Edge to CHANGE the response before Apigee Edge sends it to the original client. For example you could add or remove a response header. You could modify the content type from JSON to XML. and so on.

So I think you'll neefd to provide more details before anyone can help you.

AND ALSO - you are asking a totally new question, but you're doing it in a comment to an answer that is over 2 years old.

Please just ask a new question.

Hi @shawkyfoda ,

Even I am stuck with the same problem. Redirect is successful with 200 response but no body is received. Could you please help me if you have found the answer?