Ubidots Community

Ubifunction to ingest sensor data using a HTTP Get request

Hi, is there a way to write a Ubifunction to ingest sensor data using a HTTP Get request in this format?

-H “X-Auth-Token:[TOKEN]” -H “Content-Type:application/json” -d “{“variable”:{“value”:10}}” -X POST “https://parse.ubidots.com/prv/YOUR-USERNAME/post-data?&device_label=ABCD

The server I am trying to receive data from cannot HTTP Post the data in the format in this tutorial.

Hi there,
thanks for sharing your question with the community.

To your question, yes. To do so, the variables and values must be sent as query parameters, and no body should be sent. For instance “https://parse.ubidots.com/prv/YOUR-USERNAME/post-data?device_label=ABC&temperature=12&humidity=23"

Following up, can you please bring an example of the format that the server would send to Ubidots?

In addition, please keep in mind that the UbiFunction module is available from the IoT Entrepreneur license and above. In case you own a STEM then I’d invite you to upgrade your account.

Hi Sergio, thanks for getting back to me. The server I am sending data from cannot send the parameters in the url, but can send it in JSON format in the body. I have attached an example JSON packet. Can this be received in Ubifunctions?

@dm3 thanks for sharing the info.

Yes, it can be received in the UbiFunctions module, however the POST method is currently only available for Professionals users and above. would you envision to deploy your solution to customers, or is it more a personal hobbyist project?

That is good news. We are planning on using this platform as a solution for our current and future customers. We would like to have our devices connected soon as we have some customers lined up and this would be a great platform for them. We are currently on the trial, do we need to upgrade to the professional plan to get this going?

Hello @dm3

If you are already in a trial then you don’t need to upgrade yet. UbiFunctions, while in a trial, allows you to choose the method freely: GET or POST.
To your case, it would be a POST.

–David

Thanks dsr, In the ubifunction I can get the HTTP params body from the variable “args”. Is there any way to get the headers?

@dm3,

If you’re sending custom headers different from the necessary to make the POST request, those should be also accesible from the “args” variable.
However, can you please advise which headers are you looking to access from the function?

I want to pass the Ubidots token as a header rather than as a html parameter. I have tried -H “X-Ubi-Token: 123” -H “X-Auth-Token:123”, but I cannot see any of the headers in the “args” variable.

You can access to the token sent as the header X-Auth-Token using the token key, just as follows:

def main(args):
    print(args["token"])  # Prints the Auth token
    return args

All the best