[英]Logic App - Expression to get a particular substring from a json
[英]Get cookie data from response in Logic App
試試這個尺寸。
在 Azure 門戶中,使用 .NET 堆棧創建一個新的 HttpTrigger Azure Function 並使用此代碼將其稱為 HttpProxy ,請注意此代碼應該適用於您,但如果您希望 8830603067s7488 超出范圍,則可能需要進行調整...
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);
}
現在在 LogicApps 中,添加您在上一步中創建的Azure Functions
操作和 select HttpProxy function。
對於正文,您現在需要(首先調整)將 this 作為正文傳遞,並將 select POST作為方法傳遞...
{
"url": "https://yoururl.com",
"method": "POST",
"headers": [
{
"key": "Username",
"value": "YourUsername"
},
{
"key": "Password",
"value": "YourPassword"
}
]
}
執行時,將產生一個結果,該結果在響應中也包含 cookies。 您會發現響應的正文包含在Body參數中,您可以提取和解析它。
這是我撥打我公司暴露的URL的例子...
... 是的,那里有很多,但這是結果,如您所見,響應底部有一個 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"
}
]
}
}
現在在 LogicApps 中,您可以解析 output 並根據需要獲取 cookies。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.