简体   繁体   English

使用 Azure 函数 v1 的 Http 响应中不存在内容长度标头

[英]Content Length header is not present in Http response with Azure function v1

We have azure function which is running with azure v1 (.NET Framework 4.7).我们有运行 azure v1 (.NET Framework 4.7) 的 azure 函数。 In http response, we are trying to set the content length but it is not flowing to client.在 http 响应中,我们试图设置内容长度,但它没有流向客户端。 It sets the Transfer encoding header with value as chunked.它将传输编码标头的值设置为分块。 To give glimpse of how we are setting it.让我们一瞥我们是如何设置它的。

var body = <some byte array data>
var response = new HttpResponseMessage();
response.Content = new ByteArrayContent(body);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength = body.Length;

I went through some online discussion and Azure/azure-functions-host#3765 There they talked about setting up,我经历了一些在线讨论Azure/azure-functions-host#3765在那里他们谈到了设置,

response.Headers.TransferEncoding.Add(TransferCodingHeaderValue.Parse("identity"))

But it seems to be of no use.但是好像没什么用。 Would anyone faced such kind of issue before?以前有人遇到过这种问题吗? BTW, when I ran the same code on azure function v2 (.NET Core 2.2) it works.顺便说一句,当我在 azure function v2 (.NET Core 2.2) 上运行相同的代码时,它可以工作。

Update: Transfer-Encoding header we could see through packet capture using wireshark.更新:Transfer-Encoding 标头我们可以通过使用wireshark 的数据包捕获看到。 Wireshark screenshot depicting Transfer-Encoding header even if content-length is set through code即使通过代码设置了内容长度,Wireshark 屏幕截图也描述了 Transfer-Encoding 标头

Update-2 : This is happening for function running locally.更新 2 :这是在本地运行的函数中发生的。 When we publish the function on azure it does provide the content length header.当我们在 azure 上发布该函数时,它确实提供了内容长度标头。 Locally, I guess it (v1) uses nodejs simulator which seems to have some issue.在本地,我猜它 (v1) 使用 nodejs 模拟器,这似乎有一些问题。 Anyway, will update here once we find out any workaround.无论如何,一旦我们找到任何解决方法,就会在此处更新。

Content-length can't be set if the Transfer-Encoding is set to be chunked .如果 Transfer-Encoding 设置为 chunked ,则不能设置 Content-length Transfer-Encoding: chunked isn't needed for progressive rendering. Transfer-Encoding: chunked渐进式渲染不需要Transfer-Encoding: chunked However, it is needed when the total content length is unknown before the first bytes are sent.但是,在发送第一个字节之前,当总内容长度未知时需要它。

Basically, the decimal value of the content-length in OCTETs represents both the entity-length and the transfer-length.基本上,OCTET 中内容长度的十进制值表示实体长度和传输长度。 If there is some transfer-encoding, then these two lengths will be different and consequently the content-length value should be ignored.如果有一些传输编码,那么这两个长度将不同,因此应该忽略内容长度值。

I test with your code in function v1 without Transfer-Encoding it work well too.我在没有Transfer-Encoding情况下用函数 v1 中的代码测试它也能很好地工作。

在此处输入图片说明

So if you want to set specific content length, you could remove Transfer-Encoding .所以如果你想设置特定的内容长度,你可以删除Transfer-Encoding

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

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