IMPORTANT NOTE: ‘Raw functions’ is an advanced option for users with more experience using the UbiFunctions module.

By default, UbiFunctions doesn't allow custom paths, setting the response code or receive/send custom headers. But, what if your application needs such level of customization? This is why 'Raw function' exists, it allows you to:

  • Use a custom endpoint path in addition to the UbiFunction’s native. This allows executing different routines based on the endpoint path.

  • Receive and respond custom headers. For instance, Authorization headers.

  • Set custom response codes 20X, 40X, 50X, beyond the standard 200 in UbiFunctions. This allows the HTTP client to know the exact result of the execution.

  • Receive and respond custom Content-Type headers for the request body and response: application/json, plain/text, etc.

To enable ‘Raw functions’, just toggle the option in the left-side panel of the UbiFunction.

Once toggled on, the “args“ argument received in the UbiFunction’s main function will follow the below structure:

{
"path": string, // "/prv/<YOUR_USERNAME>/<FUNCTION_NAME>/<YOUR_PATH>". Default: "/prv/<YOUR_USERNAME>/<FUNCTION_NAME>"
"headers": object, // Key-value object containing all headers of the request.
"body": string, // Body of the request
}

Note that the UbiFunction basic URL structure should remain intact, otherwise, the request will point to a different UbiFunction. For example, say we have the following UbiFunction URL:

https://parse.ubidots.com/prv/iot-expo/raw-function/API/Account/LogON

Here, 'https://parse.ubidots.com/prv/iot-expo/raw-function' is the typical UbiFunction URL, and '/API/Account/LogON' is the custom path that you'll be able to access and parse at will from the code in the “path” key of “args”.

Now, once the UbiFunction finishes its execution it will normally reply with a 200 response code if everything went ok for non-raw functions. However, with Raw functions, you can customize the response code, body, and headers. That way, the HTTP client making the request will know the precise result of the execution.
The following is the JSON structure to customize the response:

{
"status_code": int, // 20X, 40X, 50X. Default: 200
"headers": object, // Key-value object containing headers of the response. Default: {"Content-Type": "application/json"}
"body": string, // Formatted based on the Content-Type header. Default: {}
}

For example, if based out of your code logic the request is not valid, it should reply a 400 response code, and the JSON response should look like as the follows:

{
"status_code": 400,
"headers": {"Content-Type": "plain/text"},
"body": "[ERROR] Bad Request, please check your request",
}

On the other hand, if the request is valid and the execution successful, the response JSON would be like this:

{
"status_code": 200,
"headers": {"Content-Type": "plain/text"},
"body": "[INFO] Successful request",
}

Other users also found helpful:

Did this answer your question?