本文为您介绍HTTP通信相关函数的语法、说明、参数、返回值和示例。

http_request

函数详细信息,请参见下表:
项目 描述
语法 http_request(req_info)
说明 执行HTTP请求,非阻塞方式。
参数
req_info为请求配置,字典类型,字段如下:
  • (必选)addr:请求URL地址。
  • (可选)method:请求方法,默认GET。
  • (可选)retry:重试次数,默认1,上限5。
  • (可选)timeout:超时时间,默认1000ms,上限10000ms(=10s)。
  • (可选)send_headers:请求头,字典类型(k为请求头、v为请求头内容)。
  • (可选)body:请求body,长度上限65535。
返回值
  • 成功返回字典类型,键值如下:
    • code:响应码。
    • status:响应状态。
    • headers:响应头部(字典类型),返回均为小写的头名称。
    • body:响应体。
  • 失败返回false。
示例
req_info = []
set(req_info, 'addr', 'http://127.0.0.1:7088/test_http?ua=iphone')
set(req_info, 'retry', 1)
set(req_info, 'timeout', 1000)
set(req_info, 'method', 'GET')
set(req_info, 'body', 'ABC')
req_hdr = []
set(req_hdr, 'Content-Type', 'application/json; charset=utf-8')
set(req_hdr, 'Connection', 'close')
set(req_info, 'send_headers', req_hdr)
rs = http_request(req_info)
if rs {
    say(concat('code: ', get(rs, 'code')))
    say(concat('status: ', get(rs, 'status')))
    headers = get(rs, 'headers')
    age = ''
    age = get(headers, 'age')
    speed = ''
    speed = get(headers, 'speed')
    say(concat('age: ', age, ' speed: ', speed))
    say(concat('body: ', get(rs, 'body')))
}