What is POST and GET? How do you use them?

I am new to Spark (but have had some experience with Arduino) and was trying out the “Control LEDS over the 'net” example. I successfully uploaded the code from the example (see below) and tryed typing in Terminal POST /v1/devices/{myDeviceIDHere}/led but instead of doing something to the LED (they don’t make it too clear what the example is actually supposed to do) I am just prompted with -bash: POST: command not found

Here is the code:

int led1 = D0;
int led2 = D6;

// This routine runs only once upon reset
void setup()
{
   //Register our Spark function here
   Spark.function("led", ledControl);

   // Configure the pins to be outputs
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

   // Initialize both the LEDs to be OFF
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);
}


// This routine loops forever
void loop()
{
   // Nothing to do here
}


// This function gets called whenever there is a matching API request
// the command string format is l<led number>,<state>
// for example: l1,HIGH or l1,LOW
//              l2,HIGH or l2,LOW

int ledControl(String command)
{
   int state = 0;
   //find out the pin number and convert the ascii to integer
   int pinNumber = (command.charAt(1) - '0') - 1;
   //Sanity check to see if the pin numbers are within limits
   if (pinNumber < 0 || pinNumber > 1) return -1;

   // find out the state of the led
   if(command.substring(3,7) == "HIGH") state = 1;
   else if(command.substring(3,6) == "LOW") state = 0;
   else return -1;

   // write to the appropriate pin
   digitalWrite(pinNumber, state);
   return 1;
}

Please help!
Thanks,
ma7730


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

POST and GET are part of a REST API. It’s used to make requests over the web. GET will allow you to, well, get things, whereas POST will allow you to, like the name suggests, post things. You use GET went you want information from something, without changing anything at that ‘something’, and POST if you want to send information, and do something with it.

In order to issue the commands to your Spark you’ll have to use both of these for different things. Variable values can be requested with GET, and functions can be executed with POST, with the option to transfer data along with it. Depending on your OS you might need to install some software to be able to make use of these functions. Windows for example uses CURL.
To make things easier for testing, there are some online services that’ll allow you to make those request, and even some plug-ins for certain browsers. Postman for Chrome is an often used example

You could make these request with other programming languages if you like. Most programming languages have these capabilities somewhere. Since this is an IoT product, it’s perhaps most useful to choose an ‘internet programming language’, something like PHP or Javascript. There are some examples of that floating around in the tutorial section here on the forum, and we’ve also got a JavaScript SDK in the docs.
This is a pretty nice tool that shows you your Cores, functions, and Variables, and allows you to interact with them, which I’ve used quite a lot. I’ve spun up my own version of that, using the JavaScript SDK, which can be found here.

I hope some of the above had been useful, but don’t hesitate to ask if you’re looking for more information. We’re glad to help!

Good luck, and enjoy!

4 Likes

Just for the sake of commenting :wink: or to be exact POST and GET are HTTP verbs that also get used to communicate with RESTful systems.

2 Likes

Thank you both for your prompt relies. I greatly appreciate it. You have not answered my question how I was hoping, however. I uploaded the code and tried the POST command, but as I said before -bash: POST: command not found is all that happened. I do have cUrl installed. Please, how do I use them to control my spark core?
Thanks,
ma7730

POST is not a command you can just type into your command line interpreter.

As said above, it is a HTTP verb, so you need to wrap it into an HTTP request.
What OS are you using? As there might be tools like cURL for Windows and Linux for your OS too.

If you search the forum or google for curl you’ll find plenty.

Thank you again for your quick reply.
I am using a Mac, and have curl installed. It has worked with communicating with my Arduino Yún, so I know a little bit about it. I tried wrapping the POST command in curl like so: curl POST/v1/devices/{DEVICE_ID}/led
and got this:

curl: (6) Could not resolve host: POST
curl: (3) <url> malformed

How do I correctly use it?
Thanks,
ma7730

Have a look in the official docs here
http://docs.spark.io/api/

1 Like

I have already looked there. It just says:

CALLING A FUNCTION
Call a function exposed by the core, with arguments passed in request body, e.g., POST /v1/devices/0123456789abcdef01234567/brew

As I said before, I already tried that, as that is what was said to do in the example. It does not work. What exactly do I type in so that it does work?
Thanks,
ma7730

Actually it does give you some more further down here

http://docs.spark.io/api/#basic-functions-controlling-a-core

you’ll find this

POST /v1/devices/{DEVICE_ID}/{FUNCTION}

# EXAMPLE REQUEST
curl https://api.spark.io/v1/devices/0123456789abcdef01234567/brew \
     -d access_token=1234123412341234123412341234123412341234 \
     -d "args=202,230"


// for this firmware 
int brew(String args)
{
  // parse brew temperature and duration from args
  // ...

  activate_heating_element(temperature);
  start_water_pump(duration_seconds);

  // int status_code = ...
  return status_code;
}

void setup()
{
  Spark.function("brew", brew);
}
2 Likes

Hi @ma7730

In the doc, this is the example you want to follow for a HTTP POST request–here “brew” is the Spark function and it takes a string arg of “202,230”. Replace the long hex numbers with the numbers for your core and access token and the backslash “” is the line continuation character so you can type this in on multiple lines in a shell.

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/brew \ -d access_token=1234123412341234123412341234123412341234 \ -d "args=202,230"
This is what happens typically when you hit “submit” or similar buttons on a web page form.

Can I also recommend you look at the tutorial category here in the forum, especially the getting started topics, which show how to do this from a web page you can write.

1 Like

Yes, so what am I supposed to put into Terminal? I put in:

curl https://api.spark.io/v1/devices/0123456789abcdef/led \
  -d access_token=123412341234 \
  -d params=l1,HIGH

And got:

  "id": "53ff75065075535120461187",
  "name": "Relay",
  "last_app": null,
  "connected": true,
  "return_value": 1

I suppose this is getting the value from my sketch, but I was hoping for something like 1 or on, not all this other stuff.
If this is the command to be used, what is the POST there for?

Thanks,
ma7730

@bko

Thanks for your reply. So if this is what you should use, what is POST for?

HI @ma7730

You got the return value of 1 from the function. A Spark.function() always takes a string and returns an int, but all the Spark API end-points return a JSON structure with lots of interesting data so your web side can figure out what happened etc. These are easy to parse in Javascript but there are JSON parsers for lots of languages.

The curl command you ran does and HTTP POST request. The POST is just an example in the doc so if you know how web requests work, you can use your favorite tool to do an HTTP POST, like you did with curl. If you look at the tutorials. you will see how you can the same thing with Javascript on a web page. As others have said the web tool called ‘postman’ is also popular for testing.

2 Likes

This is what @Moors7 has already told you in his first reply.


Even tho' you don't see the keyword POST in this

curl https://api.spark.io/v1/devices/0123456789abcdef/led \
  -d access_token=123412341234 \
  -d params=l1,HIGH

curl does understand the parameters you provided in a way that it "knows" to produce a POST request.

1 Like

@bko
Thank you again, that cleared things up very nicely. I'm just a little confused. What do you mean by:

The POST is just an example in the doc

How do you use this example? Why is it included in the code like something I should put in the command line somewhere?

@ScruffR
I know @Moors7 alread said that I was just wondering what the point of POST was and when it should be used, if it doesn't work in the command line and you should use curl instead.
The second part of your post cleared it up. So, why is that POST command included in the example. Is there some where that goes?

Thanks,
ma7730

Maybe if I put it slightly different - less technical correct, but more understandable:

POST is not a command but rather a kind of technique to tell some internet connected devices (e.g. servers) to do things for you, while for example GET is a way to ask some internet connected device to give you something (aka get it for you).
You could also understand it like a heading in a manual for your VCR (a what? :wink: ). The headind as such doesn’t tell you what to do, but it wraps all the required actions up in one easy to remember word which can be put in the index. And if you ask some support staff for this VCR, they’ll be able to give you more details.

Does this make it a bit clearer?

Yes, thank you, exactly what I was looking for. I am not too experienced at this so when you all started giving very detailed, technical descriptions I got slightly confused.
Also, what's a VCR? :wink:

The tutorial gave me the impression that GET and POST are commands, but they aren't?
Especially this:

CALLING A FUNCTION
Call a function exposed by the core, with arguments passed in request body, e.g., POST /v1/devices/0123456789abcdef01234567/brew
POST /v1/devices/{DEVICE_ID}/{FUNCTION}[quote="ScruffR, post:16, topic:10006, full:true"]

Can you tell me what they mean? Do they just mean "a POST means..." The wording sounds a lot like a command. I'm just kinda hopeful for a command that is relatively easy to type and just return one value. :smile:

Thanks,
ma7730

think of GET and POST as types of http commands.

in reality, you are using a Spark Function, which is another way of saying you are sending a command to your SPARK... so is is no surprise that when you POST a command to your relay to turn it on:

curl https://api.spark.io/v1/devices/0123456789abcdef/led -d access_token=123412341234 -d params=l1,HIGH

it comes back with information confirming your command:

  "id": "53ff75065075535120461187",
  "name": "Relay",
  "last_app": null,
  "connected": true,
  "return_value": 1

because your Spark.function( ) is written to do that...

If you simply want to see the state of the relay, you want to use a Spark.variable( )

They're methods you use, by which you can achieve something. Different goals require different methods. When you only want data, then it suffices to GET that data. When you want to transfer and process data, you'll have to POST it. There are some other methods available for specific purposes, like PUT (update of create), and DELETE. Take a look at this W3Schools page for a general idea. Having only quickly glanced at this, it might be interesting as well, explaining the whole concept a bit more thoroughly.

Where are you planning to use your Spark? And how do you plan to control it?

@BulldogLowell
Thanks for your response, you gave me a much clearer idea.
@Moors7
Thank you for replying and the links, they provided some good insight. I am not building anything in particular right now, just trying to get a feel for the IoT and stuff.

1 Like