Set Input Fields via API in emoncms

Hello all,

I have recently embarked on a project to install emoncms and integrate it with several Z-Wave sensors via the HomeSeer JSON API. I have managed to create a PHP script that creates/updates inputs in emoncms via its JSON API. I would like to be able to update the "description" field for each of my new inputs as described here: https://github.com/emoncms/emoncms/blob/master/Modules/input/Views/input_api.php. I have run into two issues so far: one I have resolved and the other one not so much. First, I have discovered that fields passed on the URL must be enclosed in double quotes, not single quotes. Second, and the reason for this post, is that while updating fields works from a browser using authentication, it does not appear to work from a script using the API keys. I have tried various methods or passing the data:

Using the format in the docs for "Set input fields" (fails auth):

http://emon.home/emoncms/input/set.json?inputid=17&fields={"description":"Inside Temp"}

Error: {"success":false,"message":"Username or password empty"}

Using the format in the "APIKEY" section of the docs except for field update instead of data input (still fails auth even though I am passing the correct apikey):

http://emon.home/emoncms/input/set.json?inputid=17&fields={"description":"Inside Temp"}&apikey=xxx

Error: {"success":false,"message":"Username or password empty"}

Using apikey as the first field (note that auth appears to succeed in this case):

http://emon.home/emoncms/input/set.json?apikey=xxx&inputid=17&fields={"description":"Inside Temp"}

Error: {"success":false,"message":"Field could not be updated"}

Passing username/password in the URL (fails auth):

http://username:password@emon.home/emoncms/input/set.json?inputid=17&fields={"description":"Inside Temp"}

Error: {"success":false,"message":"Username or password empty"}

Setting up a context to pass username/password in PHP like here: http://www.hashbangcode.com/blog/using-authentication-and-filegetcontents (still fails auth):

http://emon.home/emoncms/input/set.json?inputid=17&fields={"description":"Inside Temp"}

Error: {"success":false,"message":"Username or password empty"}

 

Has anyone had any success doing this programmatically?

 

Thanks.

chaveiro's picture

Re: Set Input Fields via API in emoncms

Just tested from the browser and no problem:

http://server/input/set.json?inputid=2&fields={%22description%22:%22test2%22}&apikey=SOME_KEY

ian351c's picture

Re: Set Input Fields via API in emoncms

Yes, a browser works, but command line (script, wget, etc.) does not.

TrystanLea's picture

Re: Set Input Fields via API in emoncms

I think it may be that you need single quotes around the request:

wget 'http://localhost/emoncms/feed/set.json?id=1&fields={"tag":"testing"}&apikey=KEY'

also tested with a php script works ok:

<?php

file_get_contents('http://localhost/emoncms/feed/set.json?id=1&fields={"tag":"testing"}&apikey=KEY');

The return message from emoncms is not correct but it does update.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.