http_request

This function creates an HTTP request and sends it.

An HTTP request can be used for many things like (for example) authentication via HTTP headers if you use RESTful APIs. The function requires the full IP address of the server to request from as well as the type of request to make (as a string, see the note below): "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS", or "CONNECT". You will also need to supply a DS map of key/value pairs (as strings, where the key is the header field and the value is the required data for the header), and the final argument is an optional data string that you can add to the request, and if it's not needed then it can be either 0 or an empty string "". Note that you can also send a buffer (see the section on Buffers for more details), in which case the last argument would be the index of the buffer to send.

Usage Notes

This function returns an Async Request ID which can be used to identify its callback, as described below.

Async Callback

This event will generate a "callback" which is picked up by any Async HTTP Events, in which case it will generate a DS Map that is exclusive to this event and is stored in the special variable async_load. This DS map has the following two keys related to the request function:

 

Syntax:

http_request(url, method, header_map, body);

Argument Type Description
url String The web address of the server that you wish to get information from
method String The request method (normally "POST" or "GET", but all methods are supported)
header_map DS Map A DS map with the required header fields
body RealString, or Buffer The data to be transmitted following the headers (can be a binary buffer handle)

 

Returns:

Async Request ID

 

Example:

var _headers = ds_map_create();
ds_map_add(_headers, "Authorization", "Basic eW95b19hZG1pbjpjNG5lZmllbGQ=");
ds_map_add(_headers, "Content-Type", "application/x-www-form-urlencoded");
ds_map_add(_headers, "Cookie", "request_method=GET; _InAppPurchases_session=69bb6ef6eec2b");
var _data="utf8=%E2%9C%93&authenticity_token=kPmS14DcYcuKgMFZUsN3XFfj3mhs%3D&product%5Bname%5D=CatchTheHaggis&product%5Bproduct_id%5D=http_test&commit=Create+Product";
request = http_request("http://225.0.0.97:3000/products", "POST", _headers, _data);
ds_map_destroy(_headers);

The above code creates a DS map with the relevant HTTP headers for the function, then creates a data string for use as this is a POST request. Finally the function is called, with its ID value being stored in the instance variable request for checking in the HTTP asynchronous event.