Ever wonder how to perform HTTP requests from WarpScript? Depending on your needs, here are 3 ways to achieve it.
Often we need to perform HTTP requests in our scripts, for instance, to send a notification in a Slack channel or to a mailing API, to fetch data from a REST API or to send data to a tier server.
In Warp 10 2.x, there was 3 ways to perform HTTP requests in WarpScript or FLoWS.
Update: Since Warp 10 3.0, there is only one way : the HTTP function.
HTTP
HTTP allows you to send a GET or POST HTTP request (thanks Captain Obvious), waiting for a response.
Among the different options, you can even stream your data using the chunks.
Following Warp 10 3.0 trend, most of the configurations can be superseeded by capabilities.
Configuration parameters in /path/to/warp10/etc/conf.d/70--extensions.conf
:
warpscript.extension.http=io.warp10.script.ext.http.HttpWarpScriptExtension
: Activate the extensionwarpscript.http.maxrequests
: maximum parallel usagewarpscript.http.maxsize
: maximum response sizewarpscript.http.maxchunksize
: maximum chunk sizewarpscript.http.host.patterns
: allow/disallow some hosts based on a comma separated list of regular expression likeslack.com,.*!
WarpScript
First, you need to generate a token to be able to use HTTP: create a mc2 file with this content
{
'id' 'TokenRead'
'type' 'READ'
'application' 'nonexistingapp'
'owner' "c7636029-d7b5-4b7a-9aab-65d61419d719"
'issuance' 0
'expiry' [ 2200 01 01 ] TSELEMENTS->
'labels' { }
'attributes' {
'.cap:http' 'localhost,127.0.0.1,api.mysite.fr' // able to use HTTP
'.cap:http.requests' '100000' // number of HTTP call allowed per script
'.cap:http.size' '100000000' // raise HTTP download limit to 100MB
'.cap:http.chunksize' '1000000' // raise HTTP size chunk to 1MB
}
'applications' [ 'nonexistingapp' ]
} TOKENGEN
Run it. By default, TOKENGEN is not accessible, so you need an access to the server to run /opt/warp10/bin/warp10.sh tokengen /path/to/this/file.mc2
Then you can use the generated token in your script with CAPADD
, to enjoy HTTP!
'tokenWithHttpCapability' CAPADD
{
'url' 'https://hooks.slack.com/services/T01xxxxNT1/B0xxxM/bzZxxx5yHdJJ'
'method' 'POST'
'headers' { 'Content-Type' 'application/json' }
'body' {
'channel' '#alertes'
'username' 'Warp 10'
'text' "Published in #alertes from user Warp 10."
'icon_emoji' ':spock-hand:'
} ->JSON
}
HTTP
WEBCALL (deprecated)
WEBCALL is historically the first one. It allows you to send an HTTP request without waiting for a response. This is a one-way action. WEBCALL is handy to notify an HTTP Server without slowing your code execution or introducing a bottleneck.
Configuration parameters in /path/to/warp10/etc/conf.d/10-webcall.conf
:
webcall.host.patterns
: allow/disallow some hosts based on regular expression likeslack.com,!^127.0.0.1$,!^localhost$,^192.168.*
warpscript.maxwebcalls
: maximum parallel usagehttp.header.webcall.uuid
: will appear in theX-Warp10-WebCall
headerwebcall.user.agent
: "Warp10-WebCall" by default
WarpScript
'9M8DP97xxxxxxZwKm78zc' // WRITE token
'POST'
'https://hooks.slack.com/services/T01xxxxNT1/B0xxxM/bzZxxx5yHdJJ'
{ 'Content-Type' 'application/json' } // HTTP headers
{
'channel' '#alertes'
'username' 'Warp 10'
'text' "Published in #alertes from user Warp 10."
'icon_emoji' ':spock-hand:'
} ->JSON // Payload
WEBCALL
URLFETCH (deprecated)
URLFETCH allows you to perform a GET request and give you back the response, headers and status. It could be a bottleneck in your code, because it waits for the response.
For security reasons, you must be authenticated with a READ token.
By the way, this function will be deprecated in a future Warp 10 version because replaced by HTTP
.
Configuration parameters in /path/to/warp10/etc/conf.d/70--extensions.conf
:
warpscript.extension.urlfetch = io.warp10.script.ext.urlfetch.UrlFetchWarpScriptExtension
: Activate the extensionwarpscript.urlfetch.limit
: maximum parallel usage a developer can bypasswarpscript.urlfetch.limit.hard
: maximum parallel usagewarpscript.urlfetch.maxsize
: maximum response size a developer can bypasswarpscript.urlfetch.maxsize.hard
: maximum response sizewarpscript.urlfetch.host.patterns
: allow/disallow some hosts based on regular expression likeslack.com,.*!
WarpScript
'oQ4t3FJSguxxxxxxxxxxUCXo3vvRGg0Q5.' // READ token
AUTHENTICATE
'https://random-data-api.com/api/name/random_name?size=5' URLFETCH 0 GET 3 GET // get body
B64-> 'utf8' BYTES-> JSON-> // decode as JSON
<% 'name' GET %> F LMAP
Going further
You can now access external APIs and make Warp 10 dialog with your stack.
To simplify your usage, you can develop macros to embed those calls in order to factorize and control your code.
Read more
W. Files Conspiracy vol. 2: Spy drones over UDP
September 2020: Warp 10 release 2.7.0, ready for FLoWS
2022 review of the Warp 10 platform
Senior Software Engineer