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 https://api.spark.io/v1/devices/53ff6e065067544835540687/led -d access_token=123456789etc -d params=l0,LOW

This works on Windows

curl https://api.spark.io/v1/devices/53ff6e065067544835540687/led \ -d access_token=123456789etc \ -d params=l0,HIGH

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

https://api.spark.io/v1/devices/53ff6e065067544835540687/led?access_token=123456789etc&args=l0,HIGH

Thanks!

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:

https://api.spark.io/v1/devices/123456789etc/led?access_token=123456789etc&args=l0,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.

Thanks!
David

1 Like

Just as a follow-up

Here is a php script that handles auth and arguments

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://api.spark.io/v1/devices/my_device_ID_here/led");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "access_token=my_access_token_here&params=l0,HIGH");
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 http://www.w3schools.com/js/default.asp for helping me learn Javascript and Jquery

Hope it works for you

PDP11

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 pastebin.com 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
}

Cheers!

Hi Dorth

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

http://pastebin.com/ZA22CGKn

Dave

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
Host: api.spark.io
Authorization: Bearer 12345678
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded

args=ON

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

Thanks,
David

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.

2 Likes

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.

|https://api.simonpainter.com/core/neil/?device={device name}&function={function name}&access_token={access token}

https://api.simonpainter.com/core/neil/?device=dolly&function=up&access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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.

2 Likes

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 = "https://api.spark.io/v1/devices/**deviceIDstring**/led";
		WWWForm form = new WWWForm();
		form.AddField("arg", "off"); // or on 
		form.AddField("access_token", "**accesstokenstring**");
		WWW www = new WWW(url, form);
		StartCoroutine(WaitForRequest(www));
	}
	IEnumerator WaitForRequest(WWW www)
	{
		yield return www;
			// check for errors
			if (www.error == null)
			{
				Debug.Log("WWW Ok!: " + www.data);
			} else {
				Debug.Log("WWW Error: "+ www.error);
			}    
	}    
}

This Chrome extension can be very useful to view, edit and send requests from Chrome: https://chrome.google.com/webstore/detail/spy/aedipmheomnpcbgmanofhaccebgapije

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
http://localhost:59087/api/actionname/param1/param2/param3/
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 " http://edcapi.cornucopia.com.au/api/actionname/param1/param2/param3

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.