JS API: 404 on callFunction when argument is used

I’m using the npm particle-api-js v. 7.0.1. I have a function on my device, set_tset, which sets a temperature setpoint, and then returns the value if successfully set. I also have get_tset which returns that value from the device.

volatile double tset = 42; // default value, should be overridden
...
void setup() {
    Particle.function("set_tset", set_tset);
    Particle.function("get_tset", get_tset);
   ...
}

int set_tset(String command) {
    String s = "";
    Particle.publish("debug", s.format("API called set_tset: " + command));
    float x = -1;
    x = atof(command);
    if (x > 0) {
        tset = x;
    }
    return (int) x;
}
int get_tset(String command ) {
    String s = "";
    Particle.publish("debug", s.format("API called get_tset: |%3.3f| ", tset));
    return (int) tset;
}

This function works fine with curl,

curl -X POST 
    https://api.particle.io/v1/devices/$dev/set_tset?access_token=$tok 
    -d arg="667"

successfully sets the variable on the device. However, in the JS API, using particle.callFunction,

let config = {deviceId: this.devID, name: "set_tset", argument: arg,  
              auth: this.access_token};
particle.callFunction(config).then(...)

I get a 404 with this response:

{"ok":false,"error":"Function set_tset not found"}

Curiously, if I use callFunction with no data argument passed, it succeeds with response:
{"id":"3b0024001247353236343033","last_app":"","connected":true,"return_value":0}

I get a confirmation from the console that the function was successfully called (with no argument). The function is definitely there, as calling it with empty argument works fine.

Could you try that call with all hardcoded data to rule out issues on that end?

Got it! I was messing around with the hardcoded function, and happened to wrap the number value in quotes ("777"), and it worked. Looks like somewhere in the pipeline it does not like dealing with a raw numeric type. arg.toString() fixed it for me.

Thanks!

1 Like

Well, Particle.function does expect a String, so trying to send it an integer was long shot to begin with ;).

Yeah, I come from a Python background, and Javascript’s fast and loose typing trips me up quite frequently (not to mention that 404 is the wrong return code for an invalid argument). It’s not obvious to me from the JS documentation that an integer would or would not get converted to a string as part of the POST protocol. I guess I was under the impression that because of url-encoding everything gets processed as a string.