lua http post application/json JSON互转
lua http post json
local http=require("socket.http"); local cjson = require("cjson") local ltn12 = require("ltn12") local response_body = {} local request_body = { text = "为什么?"} request_body = cjson.encode(request_body) local res, code, response_headers = http.request{ url = "http://xxx.com/api", method = "POST", headers = { ["Content-Type"] = "application/json"; ["Content-Length"] = string.len(request_body); }, source = ltn12.source.string(request_body), sink = ltn12.sink.table(response_body), --response_body ={"ask":"为什么?","code":1,"message":"success","text":"这次问题我也不知道"} } print(res) print(code)
print("#########################222") --列子 local a = cjson.decode('{"foo":"bar"}') print(type(a)) --table print(a["foo"]) --列子结束
response_body = table.concat(response_body) --转换字符串 local a = cjson.decode(response_body) --转table print(a["text"]) --ok json 互转https://cloud.tencent.com/developer/article/1763655