Discover the HTTPPlugin. It allows you to expose an HTTP endpoint in Warp 10 and handle requests with WarpScript.
![Tutorial HTTP Plugin](https://blog.senx.io/wp-content/uploads/2018/12/tuto_HTTP.png)
A new awesome feature comes with Warp 10 2.0: HTTP Plugin!
It allows to expose an HTTP endpoint in Warp 10 and handle requests with WarpScript.
Presentation
The main idea of the HTTP Plugin is to expose an HTTP endpoint in Warp 10 which handle requests with a WarpScript.
It handles any kind of HTTP request and put a "request" structure on the top of the stack which contains much useful information like the method, the path, the parameters and so on.
Configuration
First, you have to configure and activate the plugin in configuration files.
Activate HTTP plugin by uncommenting the following line in <path_to_warp10>/etc/conf.d/80--plugins.conf
:
warp10.plugin.http = io.warp10.plugins.http.HTTPWarp10Plugin
Then configure the plugin in the dedicated file: <path_to_warp10>/etc/conf.d/80-http-plugin.conf
:
// IP the HTTP plugin will listen on
http.host = ${standalone.host}
// Port the HTTP plugin will listen on
http.port = 9000
// Directory where spec files are located
http.dir = ${standalone.home}/http
In your Warp 10 installation, create a folder called http, ie: <path_to_warp10>/http
Restart Warp 10 in order to apply your changes.
First WarpScript
At first, begin with a simple example. Create a new file call <path_to_warp10>/http/test.mc2
and add the content below:
{
// root path
'path' '/test'
// allow to get path info
'prefix' true
// true: parse payload of a POST url encoded request
// false: to parse manually the payload
'parsePayload' true
//
// actual executed code
//
'macro' <%
// save the original request into a variable
'request' STORE
// build the HTTP response
{
'status' 200
'body' $request ->JSON
'headers'
{
'Content-Type' 'application/json'
}
}
%>
}
Then, in a browser, navigate to http://localhost:9000/test/a/b/c?d=1&e=2&f=3&f=4
You may have to wait a bit before the script is loaded.
Discover how to connect Tableau and Warp 10 |
It will return a JSON string containing your request information. You should have something like that:
{
"headers": {
"Accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
],
"Upgrade-Insecure-Requests": [
"1"
],
"Connection": [
"keep-alive"
],
"User-Agent": [
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
],
"Host": [
"localhost:9000"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"Accept-Language": [
"fr,de-LI;q=0.9,de;q=0.8,pt;q=0.7,en-US;q=0.6,en;q=0.5"
]
},
"method": "GET",
"params": {
"d": [
"1"
],
"e": [
"2"
],
"f": [
"3",
"4"
]
},
"pathinfo": "/a/b/c",
"target": "/test/a/b/c"
}
Hello World
Now, we can create a bit more complicated script in order to build an "Hello World" HTML page generator. Create a new file call <path_to_warp10>/http/simple.hello.mc2
and add the content below:
{
// root path
'path' '/hello'
// allow to get path info
'prefix' true
// true: parse payload of a POST url encoded request
// false: to parse manually the payload
'parsePayload' true
//
// actual executed code
//
'macro' <%
// save the original request into a variable
'request' STORE
// retrieve the path info (after /hello)
$request 'pathinfo' GET
// extract each parts of the path info
'/' SPLIT 'res' STORE
// evince the first '/'
$res 0 REMOVE DROP 'res' STORE
// define an HTML template
<'
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello {{name}}!</h1>
</body>
</html>
'>
// interpolate template's vars
{ 'name' $res 0 GET } TEMPLATE
// save the result into a variable
'body' STORE
// build the HTTP response
{
'status' 200
'body' $body
'headers' { 'Content-Type' 'text/html' }
}
%>
}
Used WarpScript functions : STORE, GET, TEMPLATE, SUBLIST, SIZE, map creation
Discover the Lambda functions and use the power of WarpScript in simple JSON API deployments. |
Then navigate to http://localhost:9000/hello/world
![Result: Hello world!](https://blog.senx.io/wp-content/uploads/2018/10/http.png)
Going further
With this new feature, it is now possible to have easy access to Warp 10 for any device. Show graphs, show results refreshed periodically. It's up to you!
Read more
Using Warp 10 as a map tile server for Discovery
October 2021: Warp 10 release 2.9.0
WarpStudio in standalone mode
![Xavier Marin](https://blog.senx.io/wp-content/uploads/2019/04/IMG_8993.jpg)
Senior Software Engineer