Call REST API directly from browser

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:


You might want to take a look at this tutorial

The Javascript method for calling a Spark function is shown there and is pretty simple to get going.


I have written a little GET to POST proxy so you can call functions using a URL. It was originally for a mate but figured others may find it useful.

|{device name}&function={function name}&access_token={access token}

Why is the REST API not GET compatible. Some times you just want to test something and invoking a function via a Browser would have been so handy.

Please read this thread:

Curl or postman are both great alternatives.

If you want to, you can use this. I made it to quickly test things from the browser, and it seems to be working well.


Great post helped me a lot, Those of you who uses Unity3D and C#, the working solution would be like this for function variable example app:

using UnityEngine;
using System.Collections;

public class postURL : MonoBehaviour {

	void Start () {
		string url = "**deviceIDstring**/led";
		WWWForm form = new WWWForm();
		form.AddField("arg", "off"); // or on 
		form.AddField("access_token", "**accesstokenstring**");
		WWW www = new WWW(url, form);
	IEnumerator WaitForRequest(WWW www)
		yield return www;
			// check for errors
			if (www.error == null)
				Debug.Log("WWW Ok!: " +;
			} else {
				Debug.Log("WWW Error: "+ www.error);

This Chrome extension can be very useful to view, edit and send requests from Chrome:

Considering you’re affiliated with this tool, it would’ve been nice if you’d mention that…

sure. I built this plugin a few days ago.
now I realized there is some issue in network tab of Chrome and also in my extension (as soon as I use the same API).
All requests shown in order they finished.
But I think it would be much better to order them as soon as they were sent.
Some time the order makes sense.
So I’m trying to twitch to other API to solve it

Hi superjim, I am new to the mvc web api,
I have a question regarding MVC web api, i am trying to debug the web api in my local machine by invoking the url directly from browser like this
The controller’s action is actionname and the data following it are the params of it .
This endpoint requires an access token (JWT authentication) in the header of the request.
Thats where the problem is, i am not sure where to put this access token.Is it at the end of the url http://localhost:59087/api/actionname/param1/param2/param3,-H Authorization: Bearer token
or at the begining of the url
like -H "Authorization: Bearer token "

is it possible to invoke a particular action in a MVC controller directly from the browser using the url and the bearer token? if so then
can you give me a sample url with bearer token ?pls.