Home

Awesome

http.gml v2.0

A complete rewrite of http.gml. It's much nicer now.

Documentation is now located at docs.sidorakh.net/http.gml

Trust me, it's better that way

The old documentation is below for posterity


http(url, method, body, options, callback, [error], [progress])

A wrapper for http_request, returns the HTTP request ID. The callbacks passed in are called at appropriate times.

ParameterTypeDescription
urlStringA valid URL
methodStringA valid HTTP Request Method
bodyString/FormDataAn HTTP request body, either a string or FormData
optionsStruct<HTTPOptions>A struct setting various options in the request (further docs below)
callbackfunctionA function to be called if the HTTP request succeeds
errorfunction(Optional) A function to be called if the HTTP request fails
progressfunction(Optional) A function to be called when the HTTP request is still in progress

options <HTTPOptions>

PropertyTypeDescription
headersReal (ds_map)A ds_map index of headers to be sent with the HTTP reequest. If not provided, a map is created
keep_header_mapBooleanWhether or not to keep the header map (useful if the headers are reused and don't need to change often)
get_fileBooleanWhether or not to store the response in a buffer, which can be accessed in options.buffer in the progress callback, and result in the error and success callbacks)
filenameStringThe file to output to when using http_get_file
keep_form_dataBooleanWhether or not to clean up/destroy the FormData object included in a HTTP request. Ignored if no FormData object is passed in as body
bufferReal (Buffer)A buffer containing the HTTP response if the get_file flag is set
keep_bufferBooleanWhether or not to automatically delete the buffer generated by the get_file flag (default: false)
response_headersReal (ds_map)Response headers for the request, if available

callback(http_status, result, [options])

A function to be called when the HTTP request is successful

ParameterTypeDescription
http_statusRealHTTP status code
resultStringReal
optionsStruct<HTTPOptions>The options object passed into the http function (optional)

error(http_status, result, [options])

A function to be called when the HTTP request is unsuccessful

ParameterTypeDescription
http_statusRealHTTP status code
resultStringReal
optionsStruct<HTTPOptions>The options object passed into the http function (optional)

progress(content_length, size_downloaded, [options])

A function that is called when an HTTP request is still in progress (and has not completed or failed)

ParameterTypeDescription
content_lengthRealTotal response size
size_dwnloadedStringData downloaded
optionsStruct<HTTPOptions>The options object passed into the http function (optional)

FormData

A struct that implements FormData as best I can to the spec set out in rfc2045.

add_file(name, file, options)

Adds a file field to the FormData object

ParameterTypeDescription
nameStringField name
fileStringPath to file to load into FormData
optionsStruct<FormDataOptions>Options related to file

add_data(name, data)

Adds a data field to the FormData object

ParameterTypeDescription
nameStringField name
dataStringField Data

form_body

Returns the multipart/form-data encoded body and the boundary, ready for use in the HTTP function Returns: [Buffer, String]

cleanup

Cleans up any buffers created and stored in the FormData object that were not flagged with keep_buffer, except the results of the form_body function

options<FormDataOptions>

PropertyTypeDescription
file_is_bufferBooleanWhether or not the File passed in to add_file is a Buffer instead of a file/path
keep_bufferBooleanWhen using file_is_buffer, this controls whether or not the buffer is kept after the FormData object runs its cleanup() function
filenameStringFilename to use for the uploaded file (in the form field). If blank, will use the filename in the file argument, or the string "unknown" if a buffer was specified
mimetypeStringThe mimetype to mark the file as. This is sniffed from the filename if there's a filetype registered with the IANA in the get_mime_from_extension() function

Example Usage

Basic GET request

// Using the amazing yes/no API - https://yesno.wtf
http("https://yesno.wtf/api","GET","",{},function(status,result){
	result = json_parse(result);
	show_message(result.answer);
})

Uploading a file (multipart/form-data)

var form = new FormData();
form.add_file("file","codes.json");
form.add_data("foo","bar");
var headers = ds_map_create();
headers[? "X-App-Id"] = "ABC123"
http("https://enn3xyub5vujm.x.pipedream.net/", "POST", form, {headers:headers}, function(http_status,result){
	show_message(result);
},function(http_status,result){
	show_message("Error - " + result);
});

Downloading a photo of a cat

http("https://i.imgur.com/R42xZ1p.jpg","GET","",{get_file:true},
	function(status,result){
		buffer_save(result,"out.jpg");
		// We have a cat, maaaaan!
	}
);