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笔记地址

目录
相关文章
|
4月前
|
XML 存储 网络协议
kettle开发篇-Http client
kettle开发篇-Http client
559 0
|
4月前
|
应用服务中间件 网络安全 nginx
Client sent an HTTP request to an HTTPS server
Client sent an HTTP request to an HTTPS server
558 0
|
4月前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
220 0
|
4月前
|
NoSQL Java API
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
SpringBoot【ElasticSearch集成 02】Java HTTP Rest client for ElasticSearch Jest 客户端集成(依赖+配置+增删改查测试源码)推荐使用
98 0
|
Apache Android开发
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
362 0
解决Failed resolution of: Lorg/apache/http/client/methods/HttpEntityEnclosingRequestBase的方案
|
10月前
|
Docker 容器
http: server gave HTTP response to HTTPS client解决方案
http: server gave HTTP response to HTTPS client解决方案
695 0
|
12月前
|
XML JSON 网络协议
SAP ABAP Gateway Client 的 ABAP 实现,重用 HTTP Connection
SAP NetWeaver Gateway 是一种提供了根据市场标准将设备、环境和平台连接到 SAP 软件的简单方法的技术平台。 该框架支持开发以人为本的创新解决方案,将 SAP 业务软件的强大功能引入社交和协作环境、移动和平板设备以及富互联网应用程序等新体验。
|
XML JSON 网络协议
SAP ABAP Gateway Client 的 ABAP 实现,重用 HTTP Connection
SAP ABAP Gateway Client 的 ABAP 实现,重用 HTTP Connection
108 0
|
网络协议 安全 Java
HTTP Client 学习笔记 (一) 初遇篇
HTTP Client 学习笔记 (一) 初遇篇
HTTP Client 学习笔记 (一) 初遇篇
|
JSON 网络协议 Java
你会换掉Postman吗?我正在用HTTP Client...
相信大家都用过POSTMAN吧,后端在开发的时候写完接口总得去自测调用一下,看符不符合自己的预期。
178 0
你会换掉Postman吗?我正在用HTTP Client...