问题描述
我使用的是 webpy 0.34,python 2.6.6.我也在使用 mimerender.我试图在我的 http 响应中包含内容长度,但由于某种原因,标题被删除.我说删除是因为我可以很好地创建自定义标头,并且我可以在客户端上看到这些标头.但是当我尝试设置内容长度时,标头永远不会将其发送给客户端.我试过在 web.created 对象中包含标题(如图所示),我也试过使用
web.header('Content-Length', len(data))
我做错了什么和/或不了解此代码的工作原理?
render_json = lambda **args: json.JSONEncoder().encode(args)类 MyHandler:@mimerender(默认 = 'json',json = render_json,)def POST(自我):数据 = "abcd"提高 web.created(data, headers={'Content-Length': len(data)})
如果数据是分块发送的(Transfer-Encoding: chunked
),那么Content-Length
标头必须省略,根据 RFC 2616:
[截图]
如果存在 Transfer-Encoding 标头字段(第 14.41 节)并且具有除身份"以外的任何值,则传输长度为通过使用分块"传输编码(第 3.6 节)定义,除非通过关闭连接来终止消息.
如果存在 Content-Length 头字段(第 14.13 节),则其OCTET 中的十进制值表示实体长度和传输长度.不得发送 Content-Length 头字段如果这两个长度不同(即,如果传输编码头字段存在).如果收到的消息同时包含Transfer-Encoding 头域和 Content-Length 头域,后者必须被忽略.
I'm using webpy 0.34, python 2.6.6. I'm also using mimerender. I am trying to include the content-length in my http response, but for some reason the header is being removed. I say removed because I can create custom headers just fine, and I can see those headers on the client. But when I try to set content-length, the header never makes it to the client. I've tried including the header in the web.created object (as shown) and I've also tried using
web.header('Content-Length', len(data))
What am I doing wrong and/or not understanding about how this code works?
render_json = lambda **args: json.JSONEncoder().encode(args)
class MyHandler:
@mimerender(
default = 'json',
json = render_json,
)
def POST(self):
data = "abcd"
raise web.created(data, headers={'Content-Length': len(data)})
If the data is sent as chunked (Transfer-Encoding: chunked
), then the Content-Length
header must be omitted, as per RFC 2616:
这篇关于内容长度正在被剥离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!