Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write inputs using POST requests. #194

Closed
hu55a1n1 opened this issue Apr 12, 2014 · 3 comments
Closed

Write inputs using POST requests. #194

hu55a1n1 opened this issue Apr 12, 2014 · 3 comments

Comments

@hu55a1n1
Copy link

Hi,

I'm trying to update my emoncms inputs from a C program using libcurl. I was just wondering if there was a way to get the post.json to accept POST requests instead of GET requests. I wanted to avoid the problem of having to encode the JSON data before a GET request when using CURL.
Is there a simple way to implement this?

I'm quite a novice to php. Please help.

Thanks.

@euqip
Copy link

euqip commented Apr 13, 2014

Your problem is probably located in the input_controller around line 207.

There you find this code:
if ($route->action == 'post')
{
$valid = true; $error = "";
$nodeid = (int) get('node');
$error = " old".$max_node_id_limit;

It is assumed that you send the nodeid with your data.
The versions 6 and 7 did'nt send necessarilly the nodeid with the data.
In version 8, there is no default value defined for nodeid when it is'nt provided, while it was 0 in the past.
I suggest to change the code above in the following way:

    if ($route->action == 'post')
    {
        $valid = true; $error = "";

        $nodeid = (int) get('node');
        // in old version the node id is not defined so use a default 0
        if($nodeid==""){
            $nodeid=0;
        }
        $error = " old".$max_node_id_limit;

Hoping it will solve your problem.

@hu55a1n1
Copy link
Author

@euqip Thanks a lot for your reply. I'll be trying your suggested changes soon.

This is what i'm trying to acheive:
http://192.168.7.2/emoncms/input/bulk.json?data=[[0,16,107,1557,110,1445]]

And this is my C code:

int main(void)
{
CURL *easyhandle;

curl_global_init(CURL_GLOBAL_ALL);
easyhandle = curl_easy_init();

if(easyhandle) {
char *data="data=[[0,16,107,1557,110,1445]]";
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(easyhandle, CURLOPT_URL, "http://192.168.7.2/emoncms/input/bulk.json");
curl_easy_perform(easyhandle);

curl_easy_cleanup (easyhandle);
}
curl_global_cleanup();
return 0;
}

I just wanted to know if the present emoncms code for bulk data handles POST requests as well.
Thanks again!

@euqip
Copy link

euqip commented Apr 13, 2014

Noramlly the BULK process handles the gets and the posts.

The documentation is given in the code:
Examples:

    // legacy mode: 4 is 0, 2 is -2 and 0 is -4 seconds to now.
      input/bulk.json?data=[[0,16,1137],[2,17,1437,3164],[4,19,1412,3077]]
    // offset mode: -6 is -16 seconds to now.
      input/bulk.json?data=[[-10,16,1137],[-8,17,1437,3164],[-6,19,1412,3077]]&offset=-10
    // time mode: -6 is 1387730121
      input/bulk.json?data=[[-10,16,1137],[-8,17,1437,3164],[-6,19,1412,3077]]&time=1387730127
    // sentat (sent at) mode:
      input/bulk.json?data=[[520,16,1137],[530,17,1437,3164],[535,19,1412,3077]]&offset=543

    See pull request for full discussion:
    https://github.com/emoncms/emoncms/pull/118
    */
    if ($route->action == 'bulk')
    {
        $valid = true;            
        if (!isset($_GET['data']) && isset($_POST['data']))
        {
            $data = json_decode(post('data'));
        }
        else 
        {
            $data = json_decode(get('data'));
        }

But to send json data , the sender needs to be identified. The identification may be done with the session created after login when using HTML interface. The JSON data sent without a session are attributed to a specific user with the provides WRITEAPIKEY the API key test is done already in the index file, with following code:

// 3) User sessions
require "Modules/user/rememberme_model.php";
$rememberme = new Rememberme($mysqli);
require("Modules/user/user_model.php");
$user = new User($mysqli,$redis,$rememberme);
require("Modules/org/org_model.php");
$org = new Org($mysqli,$redis,$rememberme);

if (get('apikey')){
    $session = $user->apikey_session($_GET['apikey']);
} else {
    $session = $user->emon_session_start();
}

If you add the write apikey in the string sent in the POST or in the GET, you will probably succeed.
try this:
http://192.168.7.2/emoncms/input/bulk.json?apikey=apikey_value&data=[[0,16,107,1557,110,1445]]

replace apikey_value with your own apiwrite key, and try it with your favorite browser. with the bulk process, the node id seems to be the second data item, item[1]. You will see the result in the emoncms Input interface, with new data sent from node 16.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants