Skip to content

net

The net module enables making HTTP requests and URL encoding/decoding.

Methods

net.client(method: string, url: string)

Creates a client for an HTTP request.

  • method: HTTP method (e.g. 'GET', 'POST')
  • url: The target URL

Returns a client object to chain request configuration:

js
let netResponse = net.client('POST', 'http://exampledomain/api')
    .params({
        'param_1': [1,2,3],
        'param_2': {'name': 'John'}
    })
    .headers({
        'Authorization': 'secret'
    })
    .contentType('application/json')
    .connect();

Alternatively:

js
// Optional syntax
let netResponse = net.get().params().connect();

.params(object)

Sets request parameters.

js
.params({ key: 'value' })

.headers(object)

Sets custom HTTP headers.

js
.headers({ 'Authorization': 'Bearer token' })

.contentType(type: string)

Sets the Content-Type header.

js
.contentType('application/json')

.connect()

Sends the configured HTTP request and returns the response.

js
.connect()

Response Object

The response from connect() exposes:

  • statusCode
  • statusMessage
  • headers()
  • contentType
  • getBytes() – binary data
  • getString() – string data
  • getJSON() – JSON-parsed data

Example:

js
const statusCode = netResponse.statusCode;
const statusMessage = netResponse.statusMessage;

const responseHeaders = netResponse.headers();
const contentType = netResponse.contentType;

const binaryData = netResponse.getBytes();
const stringData = netResponse.getString();
const jsonData = netResponse.getJSON();

Utility Methods

net.urlEncode(value: string, encoding: string)

Encodes a string for use in a URL.

js
let encodedString = net.urlEncode('asd asd', 'UTF-8');
write('encodedString', encodedString); 
// expected: asd%20asd

net.urlDecode(value: string)

Decodes a URL-encoded string.

js
let decodedString = net.urlDecode('asd%20asd');
write('decodedString', decodedString); 
// expected: asd asd