如何处理 HTTP 操作上的 GZip/Deflate
How to handle GZip/Deflate on HTTP Actions
我默认有an HTTP Action in Azure Logic Apps that calls a StackExchange API, fundamentally, it could be any API that returned GZip or Deflate个内容:
因为响应既不是 Plain Text nor JSON HTTP 操作的输出也是:
{
"$content-encoding": "gzip",
"$content-type": "application/json; charset=utf-8",
"$content": "H4sIAAAAAAAEAGWPzY7CMAyE38XnqsrPFtq+ClpFoXghEkm6iVtWQrw7Lt3mAEd/Hs+M7+AIfYb+cIeAN2MHcjOaKWNiKCqgSPa6zZ2SFRzt6YzZjJiMd2EiZF0tvjbpuoZe7rrdxuZIC9KNLo5D9B4DLUIl2gpsyDfOeLflvN8JM7kYPnbF6/+WA/ZNYcOAI+Hp5V/eCKv0heVGSwD0SnRcZXQm4ewyM+hBCbmvda3aWjVaS3h8V3Cx2fiYuMePvWZcSrKV8faPSyzF1jmhty64cGbnVjyeRIHcnG0BAAA="
}
如果你不厌其烦地通过 @base64toString()
传递 $content
字段,你最终会得到 JSON 的 Gzip 二进制表示,就我而言可以拿。
问题:我怎样才能强制 HTTP 操作像 HttpClient
一样运行并接受 GZip 数据并从操作中发出 JSON,或者更费力地获取 GZip Base64/Binary 数据并在进一步操作之前解压缩它?
我无法找到直接解决 "solve" 问题的方法,所以我创建了一个 Azure Function that made the request to the URL with the appropriate HttpClientHandler
passed into the HttpClient
:
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) {
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
if (data.url == null) {
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = "Please pass a URL in the input object"
});
}
HttpClientHandler handler = new HttpClientHandler() {
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var client = new HttpClient(handler);
var result = await client.GetAsync((string)data.url);
var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = result.Content;
return response;
}
通过交换原始问题中的内置 HTTP 操作,我能够使它正常工作:
Azure函数的配置如下:
我默认有an HTTP Action in Azure Logic Apps that calls a StackExchange API, fundamentally, it could be any API that returned GZip or Deflate个内容:
因为响应既不是 Plain Text nor JSON HTTP 操作的输出也是:
{
"$content-encoding": "gzip",
"$content-type": "application/json; charset=utf-8",
"$content": "H4sIAAAAAAAEAGWPzY7CMAyE38XnqsrPFtq+ClpFoXghEkm6iVtWQrw7Lt3mAEd/Hs+M7+AIfYb+cIeAN2MHcjOaKWNiKCqgSPa6zZ2SFRzt6YzZjJiMd2EiZF0tvjbpuoZe7rrdxuZIC9KNLo5D9B4DLUIl2gpsyDfOeLflvN8JM7kYPnbF6/+WA/ZNYcOAI+Hp5V/eCKv0heVGSwD0SnRcZXQm4ewyM+hBCbmvda3aWjVaS3h8V3Cx2fiYuMePvWZcSrKV8faPSyzF1jmhty64cGbnVjyeRIHcnG0BAAA="
}
如果你不厌其烦地通过 @base64toString()
传递 $content
字段,你最终会得到 JSON 的 Gzip 二进制表示,就我而言可以拿。
问题:我怎样才能强制 HTTP 操作像 HttpClient
一样运行并接受 GZip 数据并从操作中发出 JSON,或者更费力地获取 GZip Base64/Binary 数据并在进一步操作之前解压缩它?
我无法找到直接解决 "solve" 问题的方法,所以我创建了一个 Azure Function that made the request to the URL with the appropriate HttpClientHandler
passed into the HttpClient
:
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) {
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
if (data.url == null) {
return req.CreateResponse(HttpStatusCode.BadRequest, new {
error = "Please pass a URL in the input object"
});
}
HttpClientHandler handler = new HttpClientHandler() {
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var client = new HttpClient(handler);
var result = await client.GetAsync((string)data.url);
var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = result.Content;
return response;
}
通过交换原始问题中的内置 HTTP 操作,我能够使它正常工作:
Azure函数的配置如下: