[Solved] POSTing arguments to Spark using Python Google App Engine

Hello! I’m having a bit of trouble getting a POST request to send an argument from my server to the spark. Per the examples that I read in this thread, the url for my request from the server looks like this:

https://api.spark.io/v1/devices/DEVICEID/MyCoolFunction?access_token=ACCESSTOKEN&args=TestVar

The request goes through, but I don’t quite understand what happens with everything following the “args=” in the request. In the code running on my Spark, I have the following set up for testing:

int MyCoolFunction(String slot)
{
    if (slot == "TestVar") {
        slot1="Matched: " + slot;
    }
    else {
        slot1="No match: " + slot;
    }
    return 1;
}

“slot1” is then output to a display connected to my Spark so I see the results immediately. When the POST request above is called, my screen shows "No match: ", indicating that “slot” is a null argument. Should I be doing something differently in the above url so that “TestVar” (or whatever else I might want) gets passed into the Spark function?

Thanks!

hmmm… i don’t think you can call a function using a direct url like that. Pasting in a browser is a GET request.

Something like curl on command line or using http://www.hurl.it to test out should work fine.

The best way that i usually use is through spark-cli

According to the docs:

The length of the funcKey is limited to a max of 12 characters.

“MyCoolFunction” would be too long in this case, assuming you actually used that (for testing purposes perhaps).
This tool might be useful in testing your Core code, while making sure the webcode is fine. It gives you an interface which shows your functions/variables, and allows you to pass your own parameters. Give it a try and let us know if it worked for you?

@kennethlimcp, I did make it that far :slight_smile: I’m not pasting that url into a browser, I pasted it into my server code which executes a post request, not a get request.

@Moors7, good catch on the funcKey length. That was oversight on my part, the real function name is shorter than 12 characters.

I happened across this webpage which allows you to make requests to your core from the browser. When I put something into the “data” field beside my function name, it did indeed get sent to my Spark! The screen will happily display “Matched: TestVar” when I post from that site. So my question is, how do I do that from my code? Appending the “&args=TestVar” didn’t seem to do the trick, so is there anything else I could try?

1 Like

You need something like this:

# EXAMPLE REQUEST
curl https://api.spark.io/v1/devices/0123456789abcdef/brew \
     -d access_token=123412341234 \
     -d "args=coffee"
1 Like

Looking at the JavaScript from the Spark Helper, they use this:

  var baseURL = "https://api.spark.io/v1/devices/";
  // The base level run method command
  function doMethod(method, data) {
    var url = baseURL + <<Core-ID>> + "/" + funcKey;
    $.ajax({
      type: "POST",
      url: url,
      data: { access_token: <<Insert accesstoken>>, args: <<insert data/text>>},
      success: onMethodSuccess,
      dataType: "json"
    }).fail(function(obj) {
      onMethodFailure();
    });
  }

Perhaps some of the above can be of use?
If you wouldn’t mind sharing the relevant server code, we could perhaps take a look at that, which will certainly make it easier to figure out where the problem may lie?

1 Like

Thanks for both of your responses! They both pushed me in the right direction.

I’m using Google App Engine with python as the server, and recently started experimenting with the URLfetch library. Originally I was doing the following…

url = "https://api.spark.io/v1/devices/DEVICEID/CoolFunc?access_token=ACCESSTOKEN&args=TestVar"
result = urlfetch.fetch(url, method=urlfetch.POST)

…but then I referenced this post, which placed the args in its own field before passing it into the urlfetch() method. Works like a charm now :smile:
Thanks again guys!

1 Like

Great to hear it worked out for you! Would you mind if I edited the topic title to something that more accurately describes the problem (I was thinking of:"[Solved]POSTing arguments to Spark using Python Google App Engine")? This will make it easier to find for people facing the same, or similar, problems :smiley:

Yeah, go for it!

Just to sum up, and hopefully give some closure! The key difference is that those arguments are not sent as GET parameters, but as part of the POST body, as form encoded data. That’s the effect of the ajax snippet above, and also the -d option to curl, and use of the python dictionary for form parameters.

2 Likes