Skip to content
Jason Bertsche edited this page Oct 14, 2017 · 8 revisions

General Primitives

make-request url method params stream-params

Reporter of [response, http_status_code]

Makes a general HTTP request of method method to URL url with the parameters given by params and stream-params. With make-request and the below primitives, url should be the string representing of a valid URL. method should be a string representing an HTTP request method. If the given method is capable of sending in the multipart MIME type, the request will be streamed as bytes in multipart. Be wary of sending large requests in HTTP methods that do not support the multipart MIME type. params should be a NetLogo list, with each item inside the list being yet another list—this one with exactly two items within it: a string containing a desired HTTP parameter key name, and a string containing the corresponding parameter's desired value. stream-params should be a NetLogo list, with each item inside the list being yet another list—this one with exactly two items within it: a string containing a desired HTTP parameter key name, and one of "export-interface", "export-model", "export-view", or "export-world", indicating a type of export content to be streamed as file in the request. For more information on the format of the exported content, see the documentation on this page for the corresponding prim (e.g. look at the documentation on web:export-world for information about the format of the data sent when using "export-world" for a stream parameter value).

Most other primitives in this extension essentially function as variations of this primitive. The exporting primitives generally vary this up by correlating some extra data to the key name data. Fine importing primitives generally vary this in that they use the body of the response to the HTTP request as input for some operation in NetLogo. For example, web:import-world-fine would read the HTTP response as if it were an export-world file and try to update the world state accordingly. Simple importing primitives (like web:import-world) take no HTTP parameters and perform an HTTP GET by default.

Example:

web:make-request "http://www.google.com" "POST" [["username" "jason"] ["password" "thewebextensionisawesome"] ["query" "turtles"] ["favorite_color" "green"]]

This will send an HTTP POST to http://www.google.com, with parameters username=jason, password=thewebextensionisawesome, query=turtles, and favorite_color=green. Of course, that isn't at all to say that this request would necessarily accomplish much of anything....

download-file url filepath

Command

A simple file-downloading primitive; sends an HTTP GET to url and streams the response into the file at the location given by filepath. If filepath is a directory and the tail of url is a valid filename, the file will be written out to the file at the location given by #{filepath}/#{url.tail}. If ever the primitive attempts to write a file out to a filepath that already exists, it will throw an exception.

download-file-fine url method params filepath

Command

Sends an HTTP request to url and streams the response into the file at the location given by filepath. If filepath is a directory and the tail of url is a valid filename, the file will be written out to the file at the location given by #{filepath}/#{url.tail}. If ever the primitive attempts to write a file out to a filepath that already exists, it will throw an exception.

upload-file url method params filepath

Reporter of [response, http_status_code]

Sends an HTTP request to url containing as data the contents of file given by the filepath.

Exporting Primitives

export-interface url method params

Reporter of [response, http_status_code]

Sends an HTTP request, attaching a Base64-encoded PNG data URI of the NetLogo interface as data.

export-model url method params

Reporter of [response, http_status_code]

Sends an HTTP request, attaching the .nlogo-format textual representation of the current model as data.

export-view url method params

Reporter of [response, http_status_code]

Sends an HTTP request, attaching a Base64-encoded PNG data URI of the NetLogo view as data.

export-world url method params

Reporter of [response, http_status_code]

Sends an HTTP request, attaching the GZipped export-world file text as data.

Importing Primitives

import-drawing url

Command

A simple importing primitive; sends an HTTP GET to url and interprets the response as an image to be used as the background for the NetLogo view.

import-drawing-fine url method params

Command

Sends an HTTP request, interpreting the response as an image to be used as the background for the NetLogo view.

import-model url

Command

A simple importing primitive; sends an HTTP GET to url and interprets the response as an .nlogo file to open as the new current model.

import-world url

Command

A simple importing primitive; sends an HTTP GET to url and interprets the response as export-world text for altering the state of the current model.

import-world-fine url method params

Command

Sends an HTTP request, interpreting the response as export-world text for altering the state of the current model.