lua: http post dynamic

我试图使用Lua编程语言来消费Web服务。当我运行我的脚本时,我收到http错误500。我的API是用Python Flask编写的,以下是我的Lua脚本。

http = require("socket.http");
ltn12 = require("ltn12");
param_1 = argv[1];
param_2 = argv[2];
path = "http://www.url.com:8080/api/"
body = [[ {"number_id":"b8ce37fb-2061-4aea-975b-57a8e2d355ce","caller_number":param_1,"called_number":param_2} ]]
response_body = { }
res, code, response_headers, status = http.request
{
    url = path,
    method = "POST",
    headers =
    {
          ["X-Access-Key"] = "xxxxxxxxxxxxx",
          ["Content-Type"] = "application/json",
          ["Content-Length"] = body:len()
    },
    source = ltn12.source.string(body),
}

我收到了以下Python的回溯:

Expecting value: line 1 column 70 (char 69)(185.22                                                                                                             2.62.80)
Traceback (most recent call last):
File "/root/TVS/tokens-via-sva/app/controllers/api/codes.py", line 31, in crea                                                                                                             te_code
    data = json.loads(request.data)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
     return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
     raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 70 (char 69)

问题的起因是什么?

点赞
用户7615872
用户7615872

Lua 不支持制作 HTTP 请求的常见格式。 在 Lua 中,特定的格式是将真实数据插入字符串 body = [[ .... ]] 中,而不是变量名。 例如:

body = [[ {"number_id":"b8ce37fb-2061-4aea-975b-57a8e2d355ce","caller_number":"]]..param_1..[[","called_number":"]]..param_2..[["} ]];
2020-03-24 11:51:01