In this example we will learn how to:  

  • Use URL arguments of an HTTP GET request inside a UbiParser
  • Make an HTTP POST request from a UbiParser

At the end of this example you should be able to paste a URL with this format into your browser to send data to your devices:

https://parse.ubidots.com/prv/YOUR-USERNAME/post-data?&device_label=ABCD&temperature=73&humidity=55&token=YOUR-ACCOUNT-TOKEN

Creating a UbiParser

To create a parser you need to have an active Ubidots subscription. Go to the "Device Management" tab and select "Parsers":

Create a new parser and give it a descriptive name that resembles the function the parser will be doing; in this case we called it "Post data". In the HTTPS Endpoint URL, select "GET" as the method to trigger the parser:

Coding your Parser

The following code has two functions: 

  • A main function than accepts "args" as an input. This input is a dictionary containing the URL parameters as "key":"value" pairs. For example, if we triggered the parser with this URL:
https://parse.ubidots.com/prv/YOUR-USERNAME/parser-name?hello=world&param1=message1&param2=message2

Then the main function would receive them like this:

args = {"hello":"world", "param1":"message1", "param2":"message2"}
  • A ubidotsPost function, which uses the specified token, device_label and a JSON payload as an input, in order to make an HTTP POST request to Ubidots' API.

By using these two functions, we obtain the ability to (1) map the URL parameters to individual variables inside the function (token, device_label, and a JSON payload containing the variables and values to be sent), and (2) run the ubidotsPost function to send the obtained data to Ubidots:


var request = require('request-promise');

async function main(args) {

    // Get the device label from the request URL, then erase it from the args payload

    var device_label = args.device_label;

    delete args['device_label'];

    // Get the token from the request URL, then erase it from the args payload
    var token = args.token;

    delete args['token'];
   
    // Make POST request to Ubidots
    var post_response = await ubidotsPost(token, device_label, args);

    // Pass Ubidots' API response to the parser' response

    return post_response;

}

async function ubidotsPost(token, device_label, data) {

    var options = {

        method: 'POST',

        url: 'https://industrial.api.ubidots.com/api/v1.6/devices/' + device_label + '?force=true',

        body: data,
        json: true,
        headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token': token
        }
    };
    return await request.post(options);
}

The result

If we paste the URL below into our browser, we can obtain the {"status_code": "201"} responses from Ubidots' API saying the data was posted successfully!

And then of course, if we go to our Ubidots account, we see a device with label "abdc" and the respective variables and values:

Want to share a helpful parser to teach others how to connect the dots? Feel free to post it at community.ubidots.com and help another IoT innovator achieve their goals. 

Did this answer your question?