Module http

模块功能:HTTP客户端

Info:

  • Copyright: openLuat
  • Release: 2017.10.23
  • License: MIT
  • Author: openLuat

Functions

request (method, url[, cert=nil][, head=nil][, body=nil][, timeout=30000][, cbFnc=nil][, rcvFileName=nil]) 发送HTTP请求


Functions

request (method, url[, cert=nil][, head=nil][, body=nil][, timeout=30000][, cbFnc=nil][, rcvFileName=nil])
发送HTTP请求

Parameters:

  • method string
     HTTP请求方法
     支持"GET","HEAD","POST","OPTIONS","PUT","DELETE","TRACE","CONNECT"
  • url string
     HTTP请求url
     url格式(除hostname外,其余字段可选;目前的实现不支持hash)
     |------------------------------------------------------------------------------|
     | protocol |||   auth    |      host       |           path            | hash  |
     |----------|||-----------|-----------------|---------------------------|-------|
     |          |||           | hostname | port | pathname |     search     |       |
     |          |||           |----------|------|----------|----------------|       |
     " http[s]  :// user:pass @ host.com : 8080   /p/a/t/h ?  query=string  # hash  "
     |          |||           |          |      |          |                |       |
     |------------------------------------------------------------------------------|
  • cert table [此参数可选,默认值为: nil]
    table或者nil类型,ssl证书,当url为https类型时,此参数才有意义。cert格式如下:
     {
         caCert = "ca.crt", --CA证书文件(Base64编码 X.509格式),如果存在此参数,则表示客户端会对服务器的证书进行校验;不存在则不校验
         clientCert = "client.crt", --客户端证书文件(Base64编码 X.509格式),服务器对客户端的证书进行校验时会用到此参数
         clientKey = "client.key", --客户端私钥文件(Base64编码 X.509格式)
         clientPassword = "123456", --客户端证书文件密码[可选]
     }
  • head table [此参数可选,默认值为: nil]
    nil或者table类型,自定义请求头
          http.lua会自动添加Host: XXX、Connection: short、Content-Length: XXX三个请求头
          如果这三个请求头满足不了需求,head参数传入自定义请求头,如果自定义请求头中存在Host、Connection、Content-Length三个请求头,将覆盖http.lua中自动添加的同名请求头
          head格式如下:
              如果没有自定义请求头,传入nil或者{};否则传入{head1="value1", head2="value2", head3="value3"},value中不能有\r\n
  • body [此参数可选,默认值为: nil]
    nil、string或者table类型,请求实体
          如果body仅仅是一串数据,可以直接传入一个string类型的body即可
    

    如果body的数据比较复杂,包括字符串数据和文件,则传入table类型的数据,格式如下: { [1] = "string1", [2] = {file="/ldata/test.jpg"}, [3] = "string2" } 例如上面的这个body,索引必须为连续的数字(从1开始),实际传输时,先发送字符串"string1",再发送文件/ldata/test.jpg的内容,最后发送字符串"string2"

    如果传输的文件内容需要进行base64编码再上传,请把file改成file_base64,格式如下: { [1] = "string1", [2] = {file_base64="/ldata/test.jpg"}, [3] = "string2" } 例如上面的这个body,索引必须为连续的数字(从1开始),实际传输时,先发送字符串"string1",再发送文件/ldata/test.jpg经过base64编码后的内容,最后发送字符串"string2"

  • timeout number [此参数可选,默认值为: 30000]
    请求发送成功后,接收服务器返回应答数据的超时时间,单位毫秒,默认为30秒
  • cbFnc function [此参数可选,默认值为: nil]
    执行HTTP请求的回调函数(请求发送结果以及应答数据接收结果都通过此函数通知用户),回调函数的调用形式为:
          cbFnc(result,prompt,head,body)
          result:true或者false,true表示成功收到了服务器的应答,false表示请求发送失败或者接收服务器应答失败
          prompt:string类型,result为true时,表示服务器的应答码;result为false时,表示错误信息
          head:table或者nil类型,表示服务器的应答头;result为true时,此参数为{head1="value1", head2="value2", head3="value3"},value中不包含\r\n;result为false时,此参数为nil
          body:string类型,如果调用request接口时传入了rcvFileName,此参数表示下载文件的完整路径;否则表示接收到的应答实体数据
  • rcvFileName string [此参数可选,默认值为: nil]
    保存“服务器应答实体数据”的文件名,可以传入完整的文件路径,也可以传入单独的文件名,如果是文件名,http.lua会自动生成一个完整路径,通过cbFnc的参数body传出

Returns:

  • string rcvFilePath,如果传入了rcvFileName,则返回对应的完整路径;其余情况都返回nil

Usage:

  • http.request("GET","www.lua.org",nil,nil,nil,30000,cbFnc)
    http.request("GET","http://www.lua.org",nil,nil,nil,30000,cbFnc)
    http.request("GET","http://www.lua.org:80",nil,nil,nil,30000,cbFnc,"download.bin")
    http.request("GET","www.lua.org/about.html",nil,nil,nil,30000,cbFnc)
    http.request("GET","www.lua.org:80/about.html",nil,nil,nil,30000,cbFnc)
    http.request("GET","http://wiki.openluat.com/search.html?q=123",nil,nil,nil,30000,cbFnc)
    http.request("POST","www.test.com/report.html",nil,{Head1="ValueData1"},"BodyData",30000,cbFnc)
    http.request("POST","www.test.com/report.html",nil,{Head1="ValueData1",Head2="ValueData2"},{[1]="string1",[2] ={file="/ldata/test.jpg"},[3]="string2"},30000,cbFnc)
    http.request("GET","https://www.baidu.com",{caCert="ca.crt"})
    http.request("GET","https://www.baidu.com",{caCert="ca.crt",clientCert = "client.crt",clientKey = "client.key"})
    http.request("GET","https://www.baidu.com",{caCert="ca.crt",clientCert = "client.crt",clientKey = "client.key",clientPassword = "123456"})
generated by LDoc 1.4.6 Last updated 2020-04-11 23:40:56