Call REST API directly from browser

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.


You’re close!


There’s good examples at I can attest that it works great using jQuery’s $.get(), $.post(), and $.ajax() methods with no CORS issues.

1 Like

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:

  "cmd": "VarReturn",
  "name": "led",
  "allTypes": {
    "string": " \u0000P\u0000",
    "uint32": 536891392,
    "number": 536891392,
    "double": null,
    "raw": " \u0000P\u0000"
  "result": " \u0000P\u0000",
  "coreInfo": {
    "last_app": "foo",
    "last_heard": "2013-12-22T05:14:52.805Z",
    "connected": false,
    "deviceID": "123412341234"

I’m not trying to get data from the core. I’m trying to post data (args=l1,HIGH, for example). So, I don’t think the Variable applies, right?


1 Like

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:,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!).

1 Like

@wgbartley unless my eyes are bleary, I’m using the format you suggested.,HIGH

That Chrome extension is nice. I’ve been using Fiddler as well. Still not seeing what is wrong with the URL.

Hey @benddennis ,

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";

	uri: "" + 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!)


Hi and thanks in advance for any reply!

I’m not getting the URL to work properly!

Using curl this works… \ -d access_token=123456789etc \ -d para

However this does not…,HIGH

What am I missing?

Hi @RickSanson,

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:


Hi @Dave,

I’m using a REST client called, well, RestClient (a Firefox add-on) and sending as POST.

Here is the response…

Status Code: 200 OK
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 121
Content-Type: application/json; charset=utf-8
Date: Thu, 02 Jan 2014 00:36:54 GMT
Server: nginx/1.4.2
X-Powered-By: Express
  "id": "123456789etc",
  "name": "Core_1",
  "last_app": null,
  "connected": true,
  "return_value": -1

I suppose that I don’t understand the JSON return value (-1 unsuccessful?)

This works on linux

curl -d access_token=123456789etc -d params=l0,LOW

This works on Windows

curl \ -d access_token=123456789etc \ -d params=l0,HIGH

This appears to be successful from RESTClient (and Fiddler) without actual results,HIGH


Hi @RickSanson,

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:,HIGH1

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.


1 Like

Just as a follow-up

Here is a php script that handles auth and arguments

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec ($ch);
curl_close ($ch);

Thanks @Dave!

1 Like

Ok, I try again

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.

Thank you to for helping me learn Javascript and Jquery

Hope it works for you


1 Like

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 and insert a link.

Dave O

1 Like

You can use the Chrome extension called “Rest Console”

The POST should look like this.

After that, provide your parameters on the Request Body. Make sure to select the proper content-type as application/x-www-form-urlencoded

Click on the POST button and wait for your response.

If you get this JSON on the Response Body, you probably saw your Digital output change state.

    "id": "xxxxxxxx067555039311387",
    "name": "xxxxxCore01",
    "last_app": null,
    "connected": true,
    "return_value": 1


Hi Dorth

Thank you for your help. I used Pastebin and a link to my code is here:


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:

POST /v1/devices/Clara/writeLED HTTP/1.1
Authorization: Bearer 12345678
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded


(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:

    "id": "ABCDEFG",
    "name": "Clara",
    "last_app": null,
    "connected": true,
    "return_value": 0

where ABCDEFG is the core number.

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.

1 Like

It works nice Dave, thank you so much, didn’t notice that we need a POST method to call the function :smiley:

1 Like

I’ll log a bug for that and see if we can’t make it accept both, having to set a non-json content-type header is a bit too inconvenient for my taste. :slight_smile: