This topic describes the syntax, description, parameters, and return values of HTTP functions. This topic also provides examples of these functions.

http_request

The following table describes the details about this function.
Item Description
Syntax http_request(req_info)
Feature Executes an HTTP request in non-blocking mode.
Parameter
req_info: the request configurations. Data type: dictionary. This parameter contains the following fields:
  • addr: the URL of the request. This field is required.
  • method: the request method. By default, the GET method is used. This field is optional.
  • retry: the number of retries. Default value: 1. Maximum value: 5. This field is optional.
  • timeout: the timeout period. Default value: 1000. Maximum value: 10000. Unit: ms. This field is optional.
  • send_headers: the request header. Data type: dictionary. The key of the field indicates the request header, and the value of the field indicates the content of the request header. This field is optional.
  • body: the request body. The request body cannot exceed 65,535 bytes in size. This field is optional.
Return value
  • Returns the following key-value pairs of the dictionary type if the function is executed:
    • code: the HTTP status code.
    • status: the response status.
    • headers: the response header of the dictionary type. This value is in lowercase.
    • body: the response body.
  • Returns a value of false if the function fails to be executed.
Example
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')))
}