简体   繁体   English

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

[英]Get cookie data from response in Logic App

In postman I can see my cookies and my values attached to them:在 postman 中,我可以看到我的 cookies 及其附加值: 在此处输入图像描述

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.问题是我不知道如何从响应中获取 cookie 值,因为我在响应中看不到它。 I want to know how do I get my cookie values from my HTTP Response.我想知道如何从我的 HTTP 响应中获取我的 cookie 值。 在此处输入图像描述

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...在 Azure 门户中,使用 .NET 堆栈创建一个新的 HttpTrigger Azure Function 并使用此代码将其称为 HttpProxy ,请注意此代码应该适用于您,但如果您希望 8830603067s7488 超出范围,则可能需要进行调整...

https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal 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.现在在 LogicApps 中,添加您在上一步中创建的Azure Functions操作和 select HttpProxy function。

With the body, you now need to (adjust first) pass in this as the body and select POST as the method...对于正文,您现在需要(首先调整)将 this 作为正文传递,并将 select POST作为方法传递...

{
  "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.执行时,将产生一个结果,该结果在响应中也包含 cookies。 You'll find the body of the response is contained in the Body parameter which you can extract and parse.您会发现响应的正文包含在Body参数中,您可以提取和解析它。

This is an example of me calling a URL that is exposed by my company...这是我拨打我公司暴露的URL的例子...

有效载荷

... 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... ... 是的,那里有很多,但这是结果,如您所见,响应底部有一个 cookies 数组,所有标头和响应正文...

{
    "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"
            }
        ]
    }
}

输出

Now in LogicApps, you can parse that output and get your cookies as need be.现在在 LogicApps 中,您可以解析 output 并根据需要获取 cookies。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM