Accessing MWScript scripts on the server from REST

MoneyWorks 9.2.5 introduces the option of invoking script handlers on the server.

REST requests use a moneyworks command-line client worker to process the request. Normally these do not load any scripts. You can configure your server to allow scripts to be loaded by these workers at the request of REST clients.

9.2.5 also allows the evaluate command to be invoked via POST as well as GET, so that it can receive a payload. This is only useful for requests that call a script handler that can process the payload.

Prerequisite

The folder.conf controlling the directory that the target document is located in must enable scripting.

This is done by including configuration line(s) in folder.conf:

Allow-Scripts: ScriptName OtherPrefix*
Sandbox-Scripts: no

Allow-Scripts specifies a space separated list of allowed script names. A * or @ wildcard can be used at the end of a prefix. To allow REST clients to be able to specify any script, you can specify Allow-Scripts: *. Sandbox-Scripts: no is required for scripts that make use of File_ or Curl_ functions. If Sandbox-Scripts: no is not included, then scripts are sandboxed by default. Any value other than no (case sensitive) will also sandbox scripts.

To disable script loading, remove the Allow-Scripts line.

Requesting a script to be loaded

You can request scripts to be loaded by the CLI worker, by including a MW-Enable-Scripts header, or an enable-scripts parameter in the url. The content is a space-separated list of scripts that you want loaded

MW-Enable-Scripts: My_Script
https://......evaluate?expr=My_Script:DoThing()&enable-scripts=My_Script

This will load only the named script(s). Your evaluate request will be able to use any public handlers declared in the script.

Important: Do not use the Load handler to perform the request. Implement a public handler that you call in your evaluate expression. If multiple requests are made with the same MW-Enable-Scripts header, they may use the same worker, and the Load handler will not be called again for the subsequent requests.

MW-Enable-Scripts: Preload_SMTP* My_Script_That_Needs_SMTP

This will load the standard Preload_SMTP.mwpkg package plus the additional named script in the document. A script that uses Built_In:__SendSMTPMail_WithCredentials would fail to compile if the Preload_SMTP package is not loaded.

Example

The script can use the GetHTTPRequestHeaders() and GetHTTPRequestPayload() functions to get the request headers and a POST requests's payload. If you need the actual request line the Datacentre server received, you can get it with HttpGetRequest. As is normal with evaluate, the content returned by the GET (or POST) is the result of the expression passed to evaluate. You can optionally call PutHTTPRequestResponse(httpcode, mimetype, path). If this has been called with a non-empty path, the response content of the REST request will be the contents of the file identified by the path. If you want the Content-Type header to be other than text/plain, you can pass a MIME type parameter. If the file path is in $TEMP, the server will delete the file after sending it. You can also just set the mimetype with an empty path and return the payload from the handler. The httpcode is ignored in the CLI (it is used for MoneyWorks Gold HTTP server responses)

MWScript:

on Get public
  return HttpGetRequest("http://cognito.co.nz/") // requires Preload_SMTP to compile
end

on Dump public	
  return "Headers:\n" + GetHTTPRequestHeaders() + "\n\nPayload:\n" + GetHTTPRequestPayload()
end

on Json public
  let fd = File_Open("TMP/my.json", "w")
  File_Write(fd, `{ "json": "blah" }`)
  let p = File_Path(fd)
  File_Close(fd)
  PutHTTPRequestResponse("200 OK", "application/json", p) // note the http code is actually ignored in this case; it will be 200 if no error
end

CURL:

curl -H "MW-Enable-Scripts: Preload_SMTP* MyScript" -X POST -d "thisis=My-post-payload&a=1" -u 'foldeR:passWord'  "https://server:6710/REST/user:pass@Document.moneyworks/evaluate/expr=MyScript:Dump()"
Posted in MWScript, REST | Comments Off on Accessing MWScript scripts on the server from REST