python - lua:http post动态
问题描述
我正在尝试使用 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)
问题的根源是什么?
解决方案
Lua 不支持制作 http 正文的非常常见的格式。lua 中的具体格式是在字符串 body = [[ .... ]] 中插入真实数据,而不是变量名。在这种情况下:
body = [[ {"number_id":"b8ce37fb-2061-4aea-975b-57a8e2d355ce","caller_number":"]]..param_1..[[","called_number":"]]..param_2..[["} ]];
推荐阅读
- yolov4 - 如何在 cvat 中使用 Yolo_v4-tiny 训练模型进行自动注释
- typescript - 奇怪的对象可能是 TypeScript 中的“空”错误
- c# - 如何在 C++ 中调用 ActiveX 方法
- mongodb - 更新 MongoDB 中的嵌套数组字段
- r - data.table 分组操作,列的变量名没有慢 DT[, mean(get(colName)), by = grp]
- c++ - 使用 CreateProcess windows API 启动 bat 文件
- php - nginx找不到索引总是返回404 not found
- pine-script - 警告:应在每次计算时调用函数“ord”以保持一致性
- c++ - 如何在 C++ 中将 JSTRING 转换为普通字符串?
- html - 从数组更改ngfor底部的颜色