1.4. Network API

The Network API provides utility functions to deal with IP addresses and send HTTP requests.

Functions

function httpGet(string url, string username = nil, string password = nil, int timeout = nil, bool return_content = false, bool cookie_auth = false)

Performs an HTTP GET request to the specified URL.

Return

table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.

Parameters
  • url: the URL to fetch.

  • username: for HTTP authentication.

  • password: the password for HTTP authentication.

  • timeout: maximum connection timeout in seconds.

  • return_content: enable sending response content back to the caller.

  • cookie_auth: Use basic (default) or cookie (used by ntopng) authentication

function httpPost(string url, string data, string username = nil, string password = nil, int timeout = nil, bool return_content = false, bool cookie_auth = false)

Send an HTTP POST request with url encoded data.

Return

table (RESPONSE_CODE, CONTENT_TYPE, EFFECTIVE_URL), with additional CONTENT and CONTENT_LEN if return_content is enabled on success, nil otherwise.

Parameters
  • url: the target URL.

  • data: the url encoded data to send.

  • username: for HTTP authentication.

  • password: for HTTP authentication.

  • timeout: maximum connection timeout in seconds.

  • return_content: enable sending response content back to the caller.

  • cookie_auth: Use basic (default) or cookie (used by ntopng) authentication

function postHTTPJsonData(string username, string password, string url, string json)

Send an HTTP POST request with json content.

Return

true on success, false otherwise.

Note

HTTP header “Content-Type: application/json” is sent.

Parameters
  • username: for HTTP authentication. Pass empty string to disable authentication.

  • password: for HTTP authentication. Pass empty string to disable authentication.

  • url: the target URL.

  • json: the data to post.

function send_udp_data(string host, int port, string data)

Send raw UDP data to a given host and port.

Parameters
  • host: the host IP address.

  • port: the host port.

  • data: the data to send.

function inet_ntoa(int numeric_ip)

This is the equivalent C inet_ntoa for Lua.

Return

the symbolic IP address.

Parameters
  • numeric_ip: the numeric IP address to convert.

function networkPrefix(string address, int netmask)

Apply a netmask to the specified IP address.

Return

the masked IP address.

Parameters
  • address: the IP address.

  • netmask: the network mask to apply.

function httpRedirect(string url)

Send an HTTP redirection header to the specified URL.

Note

this must be called before sending any other HTTP data.

Parameters
  • url: the URL to redirect to.

function httpPurifyParam(string str)

Purify a string from the HTTP standpoint. Used to purify HTTP params.

Note

The ourigied inout string with _ that replaced chars not allowed

Parameters
  • the: string to purify

function getservbyport(int port, string proto)

A wrapper for C getservbyport.

Return

getservbyport result on success, the port value on failure.

Parameters
  • port: service port.

  • proto: service protocol, e.g. “tcp”.