我需要解析从 HTTP GET 请求中检索到的文本。我在 NodeMCU 上使用 Lua,我不是很熟悉。
我正在使用脚本获取响应并使用此脚本一次将其拆分一行
local nStart, nEnd = string.find(c, "\n\n")
if (nEnde == nil) then
nStart, nEnd = string.find(c, "\r\n\r\n")
end
c = string.sub(c,nEnd+1)
print("length: "..string.len(c))
data = mysplit(c, "\n") -- fill the field with filenames
HTTP GET 请求看起来像这样
GET /lua/node.php?id=4022029&list HTTP/1.1
Host: mydomain.com
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)
当我打印 HTTP 响应时,它看起来像这样
HTTP/1.1 200 OK
Date: Tue, 28 Nov 2017 01:05:12 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=23p8rtds43pd1662ncm5cjhrl3; path=/
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
当我输入 mydomain.com/lua/node.php?id=4022029&list 时,我得到一个文件列表作为一个文本文本,但是这个脚本没有获取任何东西。 body 好像没有了。我在这里缺少什么?
更新,
我的脚本在从 HTTP 获取数据时工作正常,该数据未对数据进行分块编码,但我无法从分块编码中获取数据。
最佳答案
Transfer-Encoding: chunked
您要连接的服务器正在使用 chunked encoding ,这意味着 header 后面应该跟一个或多个由 block 长度和内容组成的 block 。看来您还没有读完内容,或者您正在使用的库出于某种原因无法处理分块内容。
关于apache - 如何使用 lua 获取 HTTP 响应体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47522460/