How to convert Curl to Node POST

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



Thanks!

I made a github to show:

  1. curl command
  2. node js fetch
  3. web browser fetch with local storage

explanation of the browser code here Async await new javascript webpage functions replace AJAX

Awesome share! Converting between cURL and any other language can be a pain.

I actually dug up this project someone made a little while ago. It’s a total lifesaver. I figure I’d share it here as well for all to see. It’s saved me more than once! https://curl.trillworks.com/