[英]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.