Krakend lua设置响应体返回nil

问题描述 投票:0回答:1

我写了一个lua脚本来修改我的请求的响应。如何返回 lua 脚本的响应。我收到了请求的初始响应,而不是修改后的响应。

{
"$schema": "https://www.krakend.io/schema/v3.json",
"version": 3,
"port": 9000,
"timeout": "300000s",
"cache_ttl": "4000s",
"extra_config": {
"router": {
"return_error_msg": true
}
},
"endpoints": [
{
"@comment": "Feature: GET apps by identification",
"endpoint": "/apps/{id}",
"output_encoding": "no-op",
"method": "GET",
"backend": [
{
"host": [
"http://xxx.yyy.zzz:8080"
],
"url_pattern": "/apps/{id}",
"extra_config": {
"modifier/lua-backend": {
"sources": [
"/etc/krakend/json.lua",
"/etc/krakend/Hello.lua"
],
"post": "local r = response.load(); modifyBody(r);",
"live": true,
"allow_open_libs": true
}
}
}
]
},

我的你好.lua

function  modifyBody(response)
print(response:body()) //return the body
local json_parse = json_parse(response);
print(json_parse) //return the modified body
print(response:body(json_parse)) //return the modified body
print(response) //return userdata: 0xc000618b10
return response:body(json_parse)
end

我从集装箱里得到了200:

[00] 2023/10/18 09:18:21 KRAKEND INFO: [SERVICE: Gin] Listening on port: 9000
[00] [GIN] 2023/10/18 - 09:18:27 | 200 |  312.644487ms |      172.20.0.1 | GET      "/apps/aaaaa"

print(json_parse) 日志返回格式正确的 json,但邮递员控制台是空的:

Could not get response
Error: aborted

我尝试了另一种组合:

"output_encoding": "json", //endpoint level
"encoding": "no-op", //backend level -> empty json {}
"output_encoding": "json", //endpoint level
"encoding": "json", //backend level ->

2023/10/19 15:06:33 KRAKEND ERROR: [ENDPOINT: /apps/:id] unexpected character '' at line 1 col 1
[00] [GIN] 2023/10/19 - 15:06:33 | 500 |  578.746497ms |      172.20.0.1 | GET      "/ apps  /11111"

lua api-gateway krakend
1个回答
0
投票

更改

Content-Length
解决了问题。

response:body(newBody)
response:headers("Content-Length", tostring(string.len(newBody)))
© www.soinside.com 2019 - 2024. All rights reserved.