How to forward Cookies from a send-request policy's response to backend in Azure API management service?

Fawad Arshad 25 Reputation points
2023-05-16T17:17:23.5233333+00:00

I have an operation in Azure API management platform, which calls a server for authentication, the server responds with cookies and I have to pass those cookies to the backend to be authorized.

The problem is that there is no policy or any piece of code in Microsoft Docs which allows me to do so.

Sample policy to understand the API operation more clearly:

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="response" timeout="10" ignore-error="false">
            <set-url>https://authserver.com</set-url>
            <set-method>POST</set-method>
        </send-request>
        <set-backend-service base-url="https://backend.com" />
        Now I want a policy or a piece of code which gets cookies from the above request's response and then forwards them to the backend
<value>@(((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("Set-Cookie"))</value>
        </set-header>
        <choose>
            <when condition="@(((IResponse)context.Variables["response"]).StatusCode == 200)">
                <set-header name="CSRFTOKEN" exists-action="override">
                    <value>@(((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("CSRFTOKEN"))</value>
                </set-header>
                <set-header name="Cookie" exists-action="override">
                    <value>@{
                    string rawcookie = ((IResponse)context.Variables["response"]).Headers.GetValueOrDefault("Set-Cookie");
                    string[] cookies = rawcookie.Split(';');
                    string cookie = cookies.FirstOrDefault( ss => ss.Contains("HASH_JSESSIONID"));                              
                    return cookie.Split(',')[1];}</value>
                </set-header>
            </when>
        </choose>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,455 questions
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-05-17T18:29:23.1933333+00:00

    Fawad Arshad Thanks for sharing the additional details. Python code you shared uses requests library (https://pypi.org/project/requests/) and reading API docs of the library you can pass cookies as parameter as described here. Further reviewing the source code of the repository in this line, it basically set Cookie header in HTTP header when sending request. Refer Cookie and Set-Cookie doc for more info.

    Unfortunately, there is not simple way other than passing the headers like above in APIM and code snippet you followed to set header Cookie is correct. Please let us know if you face any issues.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.