从逻辑应用中的响应中获取 cookie 数据

Get cookie data from response in Logic App

在 postman 中,我可以看到我的 cookie 及其附加值:

当我在逻辑应用程序中 运行 post 时,我得到了回复并且它有效。问题是我不知道如何从响应中获取 cookie 值,因为我在响应中看不到它。 我想知道如何从我的 HTTP 响应中获取我的 cookie 值。

根据Skin的建议,由于逻辑应用程序的一些限制,您需要使用 Azure Function。

根据Microsoft Document

If a Response action includes Cookie and Set-Cookie headers, Logic Apps removes these headers from the generated response message without showing any warning or error

可以使用Azure Function获取cookie数据响应,参考How to read cookies from the request in azure function app using python and Better way to set cookies and other repeatable headers in Javascript Http functions

试试这件尺码。

在 Azure 门户中,使用 .NET 堆栈创建一个新的 HttpTrigger Azure Functions 并使用此代码调用它 HttpProxy,请注意此代码应该适用于您,但可能需要如果您想超出其功能范围,请进行调整...

https://docs.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);
}

现在在 LogicApps 中,添加 Azure Functions 操作和 select 您在上一步中创建的 HttpProxy 函数。

对于 body,您现在需要(首先调整)将其作为 body 和 select POST[=43 传递=]作为方法...

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

执行时,将产生一个结果,该结果也包含响应中的 cookie。您会发现响应的 body 包含在您可以提取和解析的 Body 参数中。

这是我调用一个被我公司曝光的URL的例子...

... 是的,那里有很多,但这是结果,如您所见,响应底部有一组 cookie,所有 headers 和响应 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"
            }
        ]
    }
}

现在在 LogicApps 中,您可以解析该输出并根据需要获取 cookie。