Azure 函数请求 POST 正文编码
Azure Function Request POST Body Encoding
我想知道编码是否是我必须在由 http POST 触发的 Azure 函数 (v3) 中显式处理的东西 POST。
例如...哪种方法是正确的(s1、s2 或 s3):
[Function("MyFancyFunction")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
FunctionContext executionContext)
{
// What charset/encoding will be used if not specified?
var s1 = req.ReadAsString();
// Surely it can't be save to assume UTF8?
var s2 = req.ReadAsString(Encoding.UTF8);
// Use the charset of the first content-type
var ct = MediaTypeHeaderValue.Parse(req.Headers.GetValues("content-type").First());
var s3 = req.ReadAsString(ct.Encoding);
...
}
谢谢!
which approach is correct (s1, s2, or s3)
下面是我在我的环境中用于编码数据的示例代码,它对我有用,而不是我建议使用下面给出的代码的这 3 种方法。
public static async Task<string> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("SendMessage function requested");
string body = string.Empty;
using (var reader = new StreamReader(req.Body, Encoding.UTF8))
{
body = await reader.ReadToEndAsync();
log.LogInformation($"Message body : {body}");
}
log.LogInformation($"SendMessage processed.");
return body;
}
这是带有编码数据的输出
我将一个简单的测试函数和一个简单的 http POST 控制台应用放在一起。
控制台应用程序将 POST 有效负载显式编码为 Win-1252(之所以选择它,是因为它具有 0-255 的字符)。有效负载包括一个 0x0080 字符(utf-8 起始序列),如果不使用适当的编码进行解码,将会导致问题。
下面是 POST 有效载荷程序的样子:
var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, " http://localhost:7071/api/Function1");
req.Content = new StringContent("d\u0080d");
req.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
req.Content.Headers.ContentEncoding.Add("win-1252");
client.Send(req);
测试函数如下所示:
[Function("Function1")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
var s1 = req.ReadAsString(); // s1 == "d\u0080d"... yippie!
var response = req.CreateResponse(HttpStatusCode.OK);
return response;
}
最终结果是,是的,Azure 函数将注意内容类型的字符集指令并使用适当的编码。
我想知道编码是否是我必须在由 http POST 触发的 Azure 函数 (v3) 中显式处理的东西 POST。
例如...哪种方法是正确的(s1、s2 或 s3):
[Function("MyFancyFunction")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
FunctionContext executionContext)
{
// What charset/encoding will be used if not specified?
var s1 = req.ReadAsString();
// Surely it can't be save to assume UTF8?
var s2 = req.ReadAsString(Encoding.UTF8);
// Use the charset of the first content-type
var ct = MediaTypeHeaderValue.Parse(req.Headers.GetValues("content-type").First());
var s3 = req.ReadAsString(ct.Encoding);
...
}
谢谢!
which approach is correct (s1, s2, or s3)
下面是我在我的环境中用于编码数据的示例代码,它对我有用,而不是我建议使用下面给出的代码的这 3 种方法。
public static async Task<string> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("SendMessage function requested");
string body = string.Empty;
using (var reader = new StreamReader(req.Body, Encoding.UTF8))
{
body = await reader.ReadToEndAsync();
log.LogInformation($"Message body : {body}");
}
log.LogInformation($"SendMessage processed.");
return body;
}
这是带有编码数据的输出
我将一个简单的测试函数和一个简单的 http POST 控制台应用放在一起。
控制台应用程序将 POST 有效负载显式编码为 Win-1252(之所以选择它,是因为它具有 0-255 的字符)。有效负载包括一个 0x0080 字符(utf-8 起始序列),如果不使用适当的编码进行解码,将会导致问题。
下面是 POST 有效载荷程序的样子:
var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Post, " http://localhost:7071/api/Function1");
req.Content = new StringContent("d\u0080d");
req.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
req.Content.Headers.ContentEncoding.Add("win-1252");
client.Send(req);
测试函数如下所示:
[Function("Function1")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
var s1 = req.ReadAsString(); // s1 == "d\u0080d"... yippie!
var response = req.CreateResponse(HttpStatusCode.OK);
return response;
}
最终结果是,是的,Azure 函数将注意内容类型的字符集指令并使用适当的编码。