0

In postman I can see my cookies and my values attached to them: enter image description here

When I run the post in logic app, I get a response back and it works. Problem is that I have no idea how to get the cookie values from the response as I cannot see it in the response. I want to know how do I get my cookie values from my HTTP Response. enter image description here

Sunette
  • 41
  • 5

1 Answers1

0

Try this on for size.

In the Azure Portal, create a new HttpTrigger Azure Function using the .NET stack and call it HttpProxy using this code, noting that this code should work for you but may need tweaking if you want to go outside the bounds of what it does ...

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal

#r "Newtonsoft.Json"

using System.Net;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class MyKeyValuePairs
{
    [JsonProperty("key")]
    public string Key { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }

    public MyKeyValuePairs()
    {
        Key = String.Empty;
        Value = String.Empty;
    }
}

public class HttpProxySettings
{
    [JsonProperty("url")]
    public string Url { get; set; }

    [JsonProperty("method")]
    public string Method { get; set; }

    [JsonProperty("headers")]
    public List<MyKeyValuePairs> Headers { get; set; }

    public HttpProxySettings()
    {
        Headers = new List<MyKeyValuePairs>();
        Method = "POST";
    }
}
public class HttpProxyResponse
{
    [JsonProperty("statusCode")]
    public int StatusCode { get; set; }

    [JsonProperty("body")]
    public string Body { get; set; }

    [JsonProperty("headers")]
    public List<MyKeyValuePairs> Headers { get; set; }

    [JsonProperty("cookies")]
    public List<MyKeyValuePairs> Cookies { get; set; }

    public HttpProxyResponse()
    {
        Headers = new List<MyKeyValuePairs>();
        Cookies = new List<MyKeyValuePairs>();
        Body = String.Empty;
    }
}

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    var httpProxySettings = JsonConvert.DeserializeObject<HttpProxySettings>(requestBody);

    var result = new HttpProxyResponse();

    var cookiesContainer = new CookieContainer();
    var httpClientHandler = new HttpClientHandler();
    httpClientHandler.CookieContainer = cookiesContainer;

    var httpClient = new HttpClient(httpClientHandler);

    foreach (var header in httpProxySettings.Headers)
    {
        switch (header.Key.Trim().ToUpper())
        {
            case "AUTHORIZATION":
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(header.Value.Split(" ")[0], header.Value.Split(" ")[1]);
                break;

            case "ACCEPT":
                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(header.Value));
                break;

            default:
                httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                break;
        }
    }

    var uri = new Uri(httpProxySettings.Url);
    var httpMethod = HttpMethod.Get;

    switch (httpProxySettings.Method.Trim().ToUpper())
    {
        case "POST":
            httpMethod = HttpMethod.Post;
            break;

        default:
            break;
    }

    var httpRequestMessage = new HttpRequestMessage(httpMethod, uri);

    // Make the call and get the response.
    var response = await httpClient.SendAsync(httpRequestMessage);

    result.StatusCode = ((int)response.StatusCode);

    foreach (var header in response.Headers)
        foreach (var value in header.Value)
            result.Headers.Add(new MyKeyValuePairs() { Key = header.Key, Value = value });

    result.Body = await response.Content.ReadAsStringAsync();
    result.Cookies = cookiesContainer.GetCookies(uri).Select(x => new MyKeyValuePairs() { Key = x.Name, Value = x.Value }).ToList();

    return new OkObjectResult(result);
}

Now in LogicApps, add the Azure Functions action and select the HttpProxy function that you created in the last step.

With the body, you now need to (adjust first) pass in this as the body and select POST as the method ...

{
  "url": "https://yoururl.com",
  "method": "POST",
  "headers": [
    {
      "key": "Username",
      "value": "YourUsername"
    },
    {
      "key": "Password",
      "value": "YourPassword"
    }
  ]
}

When executed, that will yield a result that also contains the cookies in the response. You'll find the body of the response is contained in the Body parameter which you can extract and parse.

This is an example of me calling a URL that is exposed by my company ...

Payload

... and yes, there's a lot there but this is the result and as you can see, there's an array of cookies towards the bottom of the response, all of the headers and the response body ...

{
    "statusCode": 200,
    "headers": {
        "Transfer-Encoding": "chunked",
        "Vary": "Accept-Encoding",
        "Request-Context": "appId=cid-v1:ffaa216d-669a-4113-a9a3-a410e3ea837e",
        "Date": "Wed, 23 Mar 2022 05:08:35 GMT",
        "Content-Type": "application/json; charset=utf-8",
        "Content-Length": "2439"
    },
    "body": {
        "statusCode": 200,
        "body": "{\"d\":{\"__metadata\":{\"id\":\"https://myurl.com/DetailsSet('0')\",\"uri\":\"https://myurl.com/DetailsSet('0')\",\"type\":\"ZSERVICE_SRV.Details\"},\"ServiceId\":\"0\",\"Username\":\"S_USERNAME\",\"Parameters\":{\"__deferred\":{\"uri\":\"https://myurl.com/DetailsSet('0')/Parameters\"}},\"Result\":{\"__deferred\":{\"uri\":\"https://myurl.com/DetailsSet('0')/Result\"}}}}",
        "headers": [
            {
                "key": "Set-Cookie",
                "value": "sap-usercontext=sap-client=010; path=/"
            },
            {
                "key": "Set-Cookie",
                "value": "SAP_SESSIONID_ECX_010=fDYXEyhV_XMmF8D0F1mBCkUwMC2qZxHsqXIADTrRZY0%3d; path=/"
            },
            {
                "key": "Set-Cookie",
                "value": "TS0185a829=01b4b1e234597142bcf7f59feffcee3fd29bd758182de666f47ecf3d1d7283cc3adf0dd443e22722dd59dd653c37195ae58e78e338; Path=/; Domain=.myurl.com"
            },
            {
                "key": "Set-Cookie",
                "value": "TS01196ab8=0105b6b7b6f79ac0e8e202d4dc042de140020b1fe2cd0e455a269a3b755938e2d4e01e888775c0e8e63f82ce5abad536ce413b412e; Path=/; Secure; HTTPOnly"
            },
            {
                "key": "Set-Cookie",
                "value": "TS01c6a478=0105b6b7b6f79ac0e8e202d4dc042de140020b1fe2cd0e455a269a3b755938e2d4e01e888775c0e8e63f82ce5abad536ce413b412e; path=/; domain=.myurl.com; HTTPonly; Secure"
            },
            {
                "key": "x-csrf-token",
                "value": "E8Kc5dp0qJYbC5eardfBXA=="
            },
            {
                "key": "dataserviceversion",
                "value": "2.0"
            },
            {
                "key": "sap-metadata-last-modified",
                "value": "Wed, 30 Sep 2020 22:05:32 GMT"
            },
            {
                "key": "Cache-Control",
                "value": "no-store, no-cache"
            },
            {
                "key": "sap-processing-info",
                "value": "ODataBEP=,crp=,st=,MedCacheHub=SHM,codeployed=,softstate="
            },
            {
                "key": "sap-server",
                "value": "true"
            },
            {
                "key": "sap-perf-fesrec",
                "value": "73461.000000"
            },
            {
                "key": "Strict-Transport-Security",
                "value": "max-age=16070400; includeSubDomains"
            },
            {
                "key": "Vary",
                "value": "Accept-Encoding"
            }
        ],
        "cookies": [
            {
                "key": "sap-usercontext",
                "value": "sap-client=010"
            },
            {
                "key": "SAP_SESSIONID_ECX_010",
                "value": "fDYXEyhV_XMmF8D0F1mBCkUwMC2qZxHsqXIADTrRZY0%3d"
            },
            {
                "key": "TS01196ab8",
                "value": "0105b6b7b6f79ac0e8e202d4dc042de140020b1fe2cd0e455a269a3b755938e2d4e01e888775c0e8e63f82ce5abad536ce413b412e"
            },
            {
                "key": "TS0185a829",
                "value": "01b4b1e234597142bcf7f59feffcee3fd29bd758182de666f47ecf3d1d7283cc3adf0dd443e22722dd59dd653c37195ae58e78e338"
            },
            {
                "key": "TS01c6a478",
                "value": "0105b6b7b6f79ac0e8e202d4dc042de140020b1fe2cd0e455a269a3b755938e2d4e01e888775c0e8e63f82ce5abad536ce413b412e"
            }
        ]
    }
}

Output

Now in LogicApps, you can parse that output and get your cookies as need be.

Skin
  • 9,085
  • 2
  • 13
  • 29