Parameter formatting for function POST request (in JavaScript)

Sorry if this is elsewhere in the forum or documentation, but I’ve searched everywhere. How do I properly format the parameter list for a Particle.function in a POST request?

I’m using JavaScript/jQuery on a webpage to send a function request to an Electron. Here’s the appropriate code:

url  = "https://api.particle.io/v1/devices/1234567890/doCommand";
cmd = "read";
token = "1234567890";
data = {access_token: token, args: cmd};
$.post(url,data,function(val){
    console.log("command data received, return ="+val);
});

This POST request works (200 response, return value of 1), but the “read” command doesn’t make it to the Electron (confirmed through serial monitor debugging). When I use the Particle Dev “Cloud functions” interface, the function works fine and receives the “read” command.

So, I think I’ve isolated the issue to the formatting of the POST request, particularly line 4 above. In other examples on the forum, I’ve seen “args”, “arg”, and “params” used for the parameter list.

What am I doing wrong?

Thanks,

Eric

When searching for "function tutorial", this pops up:

I think you might need to use arg or params instead of args

Thanks to both @Moors7 and @kennethlimcp for your quick responses. I’ve reviewed the servo tutorial, and it looks like I’m doing the exact same thing. I’ve now tried arg, args, and params without any luck. Also, to remove any web/jQuery issues, I’m using Postman to send the POST request. I’m still getting a 200 response (so no device ID or token issues) and can see the function running in my serial monitor, but the function parameter’s not getting through. In my Particle code, I have:

int doCommand(String command){
    Serial.print("Web command received: ");
    Serial.println(command);

With that, I should be able to see the command string being sent in the serial monitor. And, I do if it comes from the Particle Dev Cloud function.

I’ve also tried to use Fiddler to see the POST request from the Particle Dev Cloud function, but can’t figure out how to set the Atom proxy settings or Fiddler filter settings to see the request.

Thanks, Eric

I’m taking a stab here. Maybe you need var cmd = "read";?

What happens if you try:

$.post(url,{access_token: token, args: "read"},function(val){
    console.log("command data received, return ="+val);
});

Hi @thegeographer

It looks like maybe the problem is that with a POST request, you must pass a body to give any params. You cannot just put them in the URL like you can with a GET request.

$.post( requestURL, { params: newValue, access_token: accessToken });

The name of the params (arg, args, params) does not seem to matter to the cloud.

1 Like

Argh. Thanks all for the help. It’s working now. The code snippet I started the post with actually works – it was a shortened version of the code I was using (which contained a mistake – I was sending a NULL instead of a string).

With further testing, I’ve found that the

data = {access_token: token, args: cmd};

line works with arg, args, or params – and even joe. That doesn’t matter. As @bko indicated, it has to be a POST request; GET doesn’t work. And, I never figured out the correct POST formatting for Postman – never got this debugging tool to work for me.

Thanks for your help and sorry for the simple mistake.

Eric

1 Like

This worked.