Scripting
Contents
Supported Scripting Languages
Where Scripts Can Be Used
Objects Available
DreamFactory passes in two additional objects for use in the scripts.
The Event Object
The event object contains the structured data about the event triggered (System Events Scripting) or from the API service call (Custom Scripted Services).
Note: Of all the variables provided to your script, only this event container object is writable.
Properties
The event object has the following properties:
Property | Type | Description |
---|---|---|
`event.request` | object | An object representing the inbound REST API call, i.e. the HTTP request. |
`event.response` | object | An object containing the response to an inbound REST API call, i.e. the HTTP response. |
`event.resource` | string | Any additional resource names typically represented as a replaceable part of the path, i.e. table name on a db/_table/{table_name} call. |
The `event.request` object contains all the components of the original HTTP request.
Type|Description| | ||||||
---|---|---|---|---|---|---|
string | The API version used for the request (i.e. 2.0). | | string | The HTTP method of the request (i.e. GET, POST, PUT). | | object | An array of query string parameters received with the request. | | object | The HTTP headers from the request. | | object | The body (POST body) of the request converted to an object, i.e. the `content` below presented as internally useable object. | | string | The body of the request in raw string format. | | string | The format type (i.e. json) of the raw `content` of the request. | |
Any changes to this data will overwrite existing data in the response, before further listeners are called and/or the request completes.
In `event.response` object contains the data being sent back to the client from the request.
Note: This object is only available/relevant on post_process event scripts, and Custom Scripting service scripts.
Type|Description| | ||||
---|---|---|---|---|
string | The API version used for the request (i.e. 2.0). | | integer | The HTTP status code of the response (i.e. 200, 404, 500, etc). | | object | The HTTP headers from the request. | | mixed | The body of the request as an object if the `content_type` is not set, or in raw string format. | | string | The content type (i.e. json) of the raw `content` of the request. | |
Just like `event.request`, any changes to `event.response` will overwrite existing data in the response, before it is sent back to the client.
The Platform Object
Note: The `platform` object is only available in V8Js scripts currently.
This platform object may be used to access the REST API of your instance via inline calls. This make service requests directly without requiring an HTTP call. The platform object has the following properties:
Type | Description | | ||
---|---|---|
object | An object that allows access to the instance's REST API | | object | The current configuration of the instance | | object | The current session information | |
These `platform.api` object contains methods for instance API access. This object contains a method for each type of REST verb.
Description | | ||||
GET a resource | | POST a resource | | PUT a resource | | PATCH a resource | | DELETE a resource | |
They all accept the same arguments:
platform.api.get( "service[/resource[/resource_id]]"[, payload] );
* `service` is always required. * `resource` and `resource_id` are optional and depend on your call. * `payload` is optional, but must contain a valid object for the language of the script.
You may also pass absolute URLs to these methods to retrieve external resources:
var _page = platform.api.get( "http://www.google.com" );
or
var _result = platform.api.get( "https://www.example.com/api/something_cool", {"cool":"very"} );
Calling internally however only requires the relative URL sans the `/api/v2/` portion:
// Retrieve all records from the 'Contacts' table in the 'db' database service var _records = platform.api.get( "db/_table/Contacts" );
Adding headers or other cURL options to platform.api calls
You can include an optional third argument to add cURL options to the request. For example to add headers
var options = { "CURLOPT_HTTPHEADER": ["Content-type: application/json", "Some-Other-Header: blah"] }; result = platform.api.post("http://localhost/rest/db/todo", JSON.stringify({"name":"test"}), options);
Please note the double quotation marks around the `CURLOPT_*` constant names. Unlike PHP, this is required in Javascript.
You can also add multiple cURL options like this.
var options = { "CURLOPT_HTTPHEADER": ["Content-type: application/json", "Some-Other-Header: blah"], "CURLOPT_SOMEOTHEROPTION": "someothervalue" }; result = platform.api.post("http://localhost/rest/db/todo", JSON.stringify({"name":"test"}), options);
For GET you can set the second argument, payload, to null to include the optional third argument for the cURL options.
var options = { "CURLOPT_HTTPHEADER": ["Content-type: application/json", "Some-Other-Header: blah"] }; result = platform.api.get("http://localhost/rest/db/todo", null, options);