Skip to content

Net Module

The Net module in MARS Engine allows you to send HTTP requests to external services with flexible parameters, headers, and response handling.

Sending an HTTP GET Request

js
// Example of simpler GET request
let netResponse = net.get('http://exampledomain/api')
                      .params({ q: 'search' })
                      .connect();

Reading the Response

  • Status:
js
const statusCode = netResponse.statusCode;
const statusMessage = netResponse.statusMessage;
  • Headers:
js
const responseHeaders = netResponse.headers();
const contentType = netResponse.contentType;
  • Data:
js
const binaryData = netResponse.getBytes();
const stringData = netResponse.getString();
const jsonData = netResponse.getJSON();

Other HTTP methods - POST/PUT/PATCH/DELETE

js
let netResponse = net.post('http://exampledomain/api')
                      .params({ q: 'search' })
                      .connect();
js
let netResponse = net.put('http://exampledomain/api')
                      .params({ q: 'search' })
                      .connect();
js
let netResponse = net.patch('http://exampledomain/api')
                      .params({ q: 'search' })
                      .connect();
js
let netResponse = net.delete('http://exampledomain/api')
                      .params({ q: 'search' })
                      .connect();

Optional Extended Syntax

js
let netResponse = null;
try {
  netResponse = net.client('POST', 'http://exampledomain/api')
         .params({
             'param_1': [1,2,3],
             'param_2': {'name': 'John'}
         })
         .headers({
             'Authorization': 'secret'
         })
         .contentType('application/json')
         .connect();
} catch (ex) {
    write('result', ex);
    write('message','Server error. Something went wrong.');
    exit();
}

URL Encoding and Decoding

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

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

Best‑Practice Checklist

  • ✅ Always handle exceptions with try/catch.
  • ✅ Use .contentType() to set appropriate media types.
  • ✅ Check status codes before trusting responses.
  • ✅ Sanitize and validate any parameters you send.

FAQ

QuestionAnswer
Do I need to build JSON manually?No, .params() can accept objects and arrays.
How do I add headers?Use .headers() before calling .connect().
What formats can I get back?Use .getBytes(), .getString(), or .getJSON().
Where can I read more?Right here