I am working through CONTROL LEDS OVER THE ‘NET example right now. I can get it to work using cURL from Cygwin (I’m on a Windows 7 PC). I thought I’d be able to access the REST API directly from my browser, but I am getting an "access token not found error. I’m assuming my URL (below) is malformed. Is it possible to access the REST API from the browser? If so, can someone tell me what I’m doing wrong.
That gets me a little bit further. After the “led” function call within the URL, I changed from a forward slash to a question mark. I no longer get the “access token not found” error. The LED does not turn on, however. I get the following JSON:
I missed the function call part. I haven’t done it personally, but it looks like a function call needs to be made with a POST request (using $.post() in jQuery). The actual URL would look something like: https://api.spark.io/v1/DEVICEID/FUNCTION?access_token=ACCESSTOKEN&args=1234,abcd. The Controlling a Core section on the same page (just a little farther up) gives an example. It looks like functions called via the API only accept strings as parameters, so you’ll need to make sure you function can use or parse whatever string you pass to the API.
If any of the official Spark folks read this, it may not hurt to have example URLs using query string variables along side the cURL examples.
You might want to also check out the Advanced Rest Client extension for Chrome:
You can form just about any type of request with various payloads and see all of the headers and responses. Also you can save and open requests to keep from having to type them in by hand every time.
Thanks for that! I’m a web developer by day, but I don’t get out much. That extension oughta come in real handy, real soon (well, as soon as we get over our holiday slump!).
You are right in that the URL itself is correct, but the method in the http request needs to be a “post” request. When you open a url in a browser directly, those are “get” requests. (web developers or experienced users can skip this post)
The reason to distinguish between Get / Put / Post / Delete requests has to do with desired side effects. Calling a function on your core has the potential to have side effects, where reading a variable is more like asking for a resource, etc. The downside of this is it’s a bit trickier to play with unless you’re using a web development tool that lets you specify the http request method.
If you were using node.js, here is some (really ugly example for calling a function on your core through the API)
var http = require('http');
var request = require('request');
var function_name = "led";
var function_params = "l2,HIGH";
var core_id = "your core id";
var access_token = "your token";
request({
uri: "https://api.spark.io/v1/devices/" + core_id + "/" + function_name,
method: "POST",
form: {
arg: function_params,
access_token: access_token
},
json: true
},
function (error, response, body) {
console.log(error, response, body);
});
(note, I haven’t tested this code, it’s more for reference!)
When you use curl with that url it should treat it as a “POST” request, and when you open that url in a browser it treats it as a “GET” request. Make sure you’re calling the function from something that can specify the “http request method”, a nice example is jQuery’s POST method here: http://api.jquery.com/jquery.post/
Hmm… If curl is working and the other methods aren’t, I’m guessing there is some problem with the request itself. In your earlier post you mentioned you were using the url:
Make sure you’re taking url parameters like “access_token” and “args” and you’re moving them into the post data / request body area, and not in the url.
I have written a simple webpage using Javascript and Jquery to control the LEDs on Do an D1
I have run it on my WAMP on my computer and also uploaded it to my webpage and it works.
You can post code in the preformatted text block (use the < / >) icon to insert the quote character. Or you can post your code in something like pastebin.com and insert a link.
I don’t know if this is any help to anyone, but I too had quite a struggle passing a String parameter to a Spark function. I could get it to work from the command line, but not sending my hand crafted HTTP POST request. I can cope with a bit of JavaScript but I’m not really up to speed with JQuery or AJAX and I’m not really sure how Curl constructs the HTTP requests. I do have some idea of how HTTP requests work. I fiddled around with the Postman HTTP client for Chrome and eventually found that this works:
(Not sure why the args=ON line is indented. It shouldn’t be.)
Substitute your core name for “Clara” and your authentication code for “12345678”. The String “ON” is delivered to the function “writeLED” and you get a response like this:
Note the really important part of the post request is the content-type header. If you don’t specify the content as “application/x-www-form-urlencoded” it won’t work at all. I don’t think this information is in the documentation anywhere. I think it needs to be.
Out of interest, shouldn’t the content length be specified in a header? Seems to work without it.