1

我正在使用 luasec、lua-socket执行对外部 API 的请求,并将数据(JSON 字符串)转换为带有cjson的 lua 表。我已经阅读了上述模块的文档,不幸的是,这些文档都没有帮助我解决我的问题。不能用当前帐户链接超过 2 个网站,抱歉。

摘要:我使用发布的请求函数获得响应和适当的字符串,当通过 cjson.decode 将所述字符串转换为 lua 表时,输出表不是所需的,它是我的响应标头的副本,这不是故意的.

以下代码是我执行请求的方式:

local function request (req_t)
  local res_t = {}

  resp = https.request {
    url = const.API_URL .. req_t.url,
    method = req_t.method,
    headers = req_t.headers,
    sink = ltn12.sink.table(res_t)
  }

  return table.concat(res_t), resp.headers, resp.code
end

使用以下调用

local res, headers = request({ ... })

我收到作为字符串的正确响应,但我的目标是使用它进行数据操作,因此将所述响应(字符串)转换为 lua 表

local resJson = cjson.decode(res)

不会产生正确的输出。它确实生成了一个与我的响应标头完全相同的表。这是我的终端的以下输出以及代码

When out of function type is: string

Desired response in string:
{"total_photos":221926,"photo_downloads":"186029632.0"}

When out of function type is: string

Desired response in string:
{"total_photos":221926,"photo_downloads":"186029632.0"}


After decode, type is: table

server  Cowboy
strict-transport-security   max-age=31536000
access-control-allow-headers    *
x-ratelimit-limit   50
x-ratelimit-remaining   46
x-cache-hits    0, 0
accept-ranges   bytes
access-control-request-method   *
x-request-id    ee5a74fd-2b10-4f46-9c25-5cfc53aeac6c
access-control-expose-headers   Link,X-Total,X-Per-Page,X-RateLimit-Limit,X-RateLimit-Remaining
content-type    application/json
connection  close
content-length  55
fastly-debug-digest f62d52c08b1ef74db89a66a0069f0a35c49e52230567905240dacf08c9ea1813
vary    Origin
cache-control   no-cache, no-store, must-revalidate
x-timer S1496524765.369880,VS0,VE111
x-cache MISS, MISS
x-served-by cache-iad2123-IAD, cache-mad9429-MAD
via 1.1 vegur, 1.1 varnish, 1.1 varnish
date    Sat, 03 Jun 2017 21:19:25 GMT
age 0
access-control-allow-origin *
x-runtime   0.011667

Printing header

server  Cowboy
strict-transport-security   max-age=31536000
access-control-allow-headers    *
x-ratelimit-limit   50
x-ratelimit-remaining   46
x-cache-hits    0, 0
accept-ranges   bytes
access-control-request-method   *
x-request-id    ee5a74fd-2b10-4f46-9c25-5cfc53aeac6c
access-control-expose-headers   Link,X-Total,X-Per-Page,X-RateLimit-Limit,X-RateLimit-Remaining
content-type    application/json
connection  close
content-length  55
fastly-debug-digest f62d52c08b1ef74db89a66a0069f0a35c49e52230567905240dacf08c9ea1813
vary    Origin
cache-control   no-cache, no-store, must-revalidate
x-timer S1496524765.369880,VS0,VE111
x-cache MISS, MISS
x-served-by cache-iad2123-IAD, cache-mad9429-MAD
via 1.1 vegur, 1.1 varnish, 1.1 varnish
date    Sat, 03 Jun 2017 21:19:25 GMT
age 0
access-control-allow-origin *
x-runtime   0.011667

产生所述日志的函数

local res, headers = request({ ... })

print('When out of function type is: ' ..type(res) .. '\n')
print('Desired response in string:')
print(res .. '\n')
resJson = cjson.decode(res)
print('\nAfter decode, type is: ' .. type(resJson) .. '\n')
pTable(resJson)
print('\nPrinting header\n')
pTable(headers)

pTable 只是一个将表格输出到标准输出的函数。

提前致谢

4

1 回答 1

0

贴出的函数和例程是正确的。问题出在我的打印表函数中,我以某种方式硬编码了我的标题。

于 2017-06-04T13:16:14.703 回答