test_post.lua 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. function demo_http_post_json()
  2. -- POST request 演示
  3. local req_headers = {}
  4. req_headers["Content-Type"] = "application/json"
  5. local body = json.encode({name="LuatOS"})
  6. local code, headers, body = http.request("POST","http://site0.cn/api/httptest/simple/date",
  7. req_headers,
  8. body -- POST请求所需要的body, string, zbuff, file均可
  9. ).wait()
  10. log.info("http.post", code, headers, body)
  11. end
  12. function demo_http_post_form()
  13. -- POST request 演示
  14. local req_headers = {}
  15. req_headers["Content-Type"] = "application/x-www-form-urlencoded"
  16. local params = {
  17. ABC = "123",
  18. DEF = 345
  19. }
  20. local body = ""
  21. for k, v in pairs(params) do
  22. body = body .. tostring(k) .. "=" .. tostring(v):urlEncode() .. "&"
  23. end
  24. local code, headers, body = http.request("POST","http://echohttp.wendal.cn/post",
  25. req_headers,
  26. body -- POST请求所需要的body, string, zbuff, file均可
  27. ).wait()
  28. log.info("http.post.form", code, headers, body)
  29. end