Call REST API directly from browser

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


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!)

3 Likes