HTTP client

简介: HTTP client

基础示例:

-- load the http module
http = require("socket.http")
-- Requests information about a document, without downloading it.
-- Useful, for example, if you want to display a download gauge and need
-- to know the size of the document in advance
r, c, h = http.request {
  method = "HEAD",
  url = "http://www.tecgraf.puc-rio.br/~diego"
}
-- r is 1, c is 200, and h would return the following headers:
-- h = {
--   date = "Tue, 18 Sep 2001 20:42:21 GMT",
--   server = "Apache/1.3.12 (Unix)  (Red Hat/Linux)",
--   ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT",
--   ["content-length"] = 15652,
--   ["connection"] = "close",
--   ["content-Type"] = "text/html"
-- }点击复制复制失败已复制


POST 请求:

#!/usr/bin/env lua
local http=require("socket.http");
local request_body = [[login=user&password=123]]
local response_body = {}
local client, code, response_headers = http.request{
  url = "http://httpbin.org/post",
  method = "POST",
  headers = {
    ["Content-Type"] = "application/x-www-form-urlencoded";
    ["Content-Length"] = #request_body;
  },
  source = ltn12.source.string(request_body),
  sink = ltn12.sink.table(response_body),
}
print(client)
print(code)
if type(response_headers) == "table" then
  for k, v in pairs(response_headers) do
    print(k, v)
  end
end
print("Response body:")
if type(response_body) == "table" then
  print(table.concat(response_body))
  -- local res = cjson.decode(table.concat(res))  -- 这样拿到返回JSON
else
  print("Not a table:", type(response_body))
end点击复制复制失败已复制


下载文件

local body, code = http.request(url)
if not body then error(code) end
local f = assert(io.open('/tmp/firmware.jx', 'wb')) -- open in "binary" mode
f:write(body)
f:close()点击复制复制失败已复制


注意

这样下载有问题,在 OpenWrt 环境下下载下来发现最终文件比原文件小。原因是 Lua 内存不足了。这个原因有 90% 的可能性,但不是 100% ,下载大文件的情况请使用 LuaCURL笔记地址

目录
相关文章
|
6月前
|
XML 存储 网络协议
kettle开发篇-Http client
kettle开发篇-Http client
712 0
|
6月前
|
应用服务中间件 网络安全 nginx
Client sent an HTTP request to an HTTPS server
Client sent an HTTP request to an HTTPS server
959 0
|
6月前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
385 0
|
Apache Android开发
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
381 0
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
|
6月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
111 0
|
Docker 容器
http: server gave HTTP response to HTTPS client解决方案
http: server gave HTTP response to HTTPS client解决方案
807 0
|
JavaScript 应用服务中间件 Linux
开源项目推荐:C/C++语言版本的http server和client,请关注RESTful
开源项目推荐:C/C++语言版本的http server和client,请关注RESTful
4172 0
|
网络协议 安全 Java
HTTP Client 学习笔记 (一) 初遇篇
HTTP Client 学习笔记 (一) 初遇篇
HTTP Client 学习笔记 (一) 初遇篇
|
JSON 网络协议 Java
你会换掉Postman吗?我正在用HTTP Client...
相信大家都用过POSTMAN吧,后端在开发的时候写完接口总得去自测调用一下,看符不符合自己的预期。
183 0
你会换掉Postman吗?我正在用HTTP Client...
|
关系型数据库 MySQL Java

热门文章

最新文章

下一篇
无影云桌面