Fork me on GitHub

HTTP

This Mongoose OS library implements HTTP API, both client and server.

C API Reference mgos_http_server.h

#include "mgos_http_server.h"


bool mgos_http_server_init(void);

struct mg_connection *mgos_get_sys_http_server(void);

void mgos_register_http_endpoint(const char *uri_path,
                                 mg_event_handler_t handler, void *user_data);

Register HTTP endpoint handler handler on URI uri_path

void mgos_register_http_endpoint_opt(const char *uri_path,
                                     mg_event_handler_t handler,
                                     struct mg_http_endpoint_opts opts);

JAVASCRIPT API Reference api_http.js

load("api_http.js");


URL.parse(url)

Parse URL string, return and object with ssl, addr, uri keys.

Example:

print(JSON.stringify(URL.parse('https://a.b:1234/foo?bar')));
// Prints: {"uri":"/foo?bar","addr":"a.b:1234","ssl":true}

HTTP.query(options);

Send HTTP request. Options object accepts the following fields: url - mandatory URL to fetch, success - optional callback function that receives reply body, error - optional error callback that receives error string, data - optional object with request parameters. By default, GET method is used. If data is specified, POST method is used, the data object gets JSON.stringify()-ed and used as a HTTP message body.

In order to send HTTPS request, use https://... URL. Note that in that case ca.pem file must contain CA certificate of the requested server.

Example:

HTTP.query({
  url: 'http://httpbin.org/post',
  headers: { 'X-Foo': 'bar' },     // Optional - headers
  data: {foo: 1, bar: 'baz'},      // Optional. If set, JSON-encoded and POST-ed
  success: function(body, full_http_msg) { print(body); },
  error: function(err) { print(err); },  // Optional
});