3
$\begingroup$

The following shell script is used to access an IoT device that returns a JSON string, the content of which is not important for this question.

#!/bin/sh
secret= <not_telling>
id="e00fce688e6bcd0bad0763e7"
curl -k "https://api.particle.io/v1/devices/$id/peek" -d access_token=$<not_telling> -d args=5

I would like to reproduce this functionality in Mathematica

(* id and secret have been defined *)
req = HTTPRequest[
  "https://api.particle.io/v1/devices/" <> id <> "/peek",
  <|"Method" -> "POST", 
   "Query" -> {"args" -> "5", "access_token" -> secret}|>]
URLExecute@req

This code does successfully POST; however it behaves as if the "-d args=5" portion of the query is missing or ignored. I'm likely missing something trivial, but cannot place my finger on it.

$\endgroup$
3
$\begingroup$

The curl request is passing the arguments as a form body, but the Mathematica expression is passing query arguments in the URL. Try changing the request property "Query" to "Body" to match the curl request:

req = HTTPRequest[
  "https://api.particle.io/v1/devices/" <> id <> "/peek",
  <|"Method" -> "POST", 
    "Body" -> {"args" -> "5", "access_token" -> secret}|>]
URLExecute@req
$\endgroup$
1
  • $\begingroup$ Very nice catch. While you were solving my problem, I was reproducing the same (incorrect) behavior in python. $\endgroup$ May 21 '20 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.