Took me a fair bit of time to figure out how to convert a curl command to a Nodejs POST command. So I thought I would share what I found.
Trying to convert this “curl” command: where f1234f1234 is the particle ID
curl https://api.particle.io/v1/devices/f1234f1234/myTogglePin -d access_token=c43212c4321c4321 -d arg=SpecialStuff
Using this .ino file
void setup() {
pinMode(D7, OUTPUT);
Particle.function("myTogglePin", myTogglePinFunction);
}
void loop() {
}
int myTogglePinFunction(String myCommand) {
digitalWrite(D7, !digitalRead(D7));
Particle.publish("myCommand variable is: ", myCommand, 60, PRIVATE);
return digitalRead(D7);
}
Works in node by using this code (assuming you have installed request)
npm install request
.
.
var request = require('request');
var options = {
url: 'https://api.particle.io/v1/devices/f1234f1234/myTogglePin',
method: 'POST',
form: {
access_token:'c43212c4321c4321',
arg:'SpecialStuff'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);