πŸ“¦ Archived documentation: r1 (committed 2026-07-24) View current documentation β†’

http

HTTP client requests and an on-device JSON web server under /app/.

The http object provides both directions of HTTP:

  • Client β€” http.req() sends requests to external servers or to other devices (including other Q5 units running a script web server).
  • Server β€” http.handle() registers a handler for requests arriving at http://<device IP>/app/<url>, letting the script expose its own JSON REST API.

Payloads can optionally be end-to-end encrypted with AES. Set the passphrase with setEnv("http.aesKey", "myAesPassphrase") β€” if not set, the device password is used.

Functions

http.req()

http.req(url, payload, timeout, encrypt) β†’ { err, code, payload }

Sends an HTTP request; a non-empty payload is sent as a POST body.

Performs an HTTP request and returns the response. Passing a payload string sends a POST with that body; an empty string "" performs a plain GET-style request.

Check res.err === 0 and res.code === 200 before trusting the response, then parse JSON payloads with JSON.parse(res.payload).

Arguments

NameTypeRequiredDefaultDescription
url string yes β€” Target URL, e.g. "http://172.16.220.39/app/abc".
payload string yes β€” POST body. Build with string(obj). Use "" for no payload.
timeout number (ms) yes β€” Request timeout in milliseconds.
encrypt boolean yes β€” Enable AES end-to-end encryption of the payload (key from setEnv("http.aesKey", …)).

Returns

object { err, code, payload } β€” err 0 and code 200 indicate success; payload is the response body text.

Example

let request = { msg: "hello from client", epoch: datetime().epoch };

let res = http.req("http://172.16.220.39/app/abc",
                   string(request), // POST payload
                   1000,            // timeout ms
                   false);          // no encryption

if(res.err === 0 && res.code === 200){
  let payload = JSON.parse(res.payload);
  print("Remote Q5 Battery Voltage", payload.batteryVoltage);
} else {
  print("Error reading data from remote Q5:", string(res));
}

http.handle()

http.handle(handler, encrypt)

Registers a request handler, turning the script into a JSON web server at /app/<url>.

Registers a callback that serves requests to http://<device IP>/app/<url>. The handler receives a request object (with req.url set to the path after /app/) and returns the response body string β€” build JSON responses with string(obj).

Register the handler once, then keep the script alive with a run() loop.

Arguments

NameTypeRequiredDefaultDescription
handler function yes β€” Callback function(req){ ... return responseString; }. req.url is the path after /app/.
encrypt boolean yes β€” Require AES end-to-end encryption (key from setEnv("http.aesKey", …)).

Example

function handleHttp(req){
  if(req.url === 'abc'){
    return string({ batteryVoltage: 14.8, io: io, uptime: now(), epoch: datetime().epoch });
  }
  return "Invalid Request URL, try /app/abc";
}

http.handle(handleHttp, false);

while(run()){ waitMS(1000); }