Message Boards Message Boards

0
|
5310 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Equivalent HTTPRequest for given curl command?

Posted 4 years ago

I have this curl command:

curl -X POST https://api.nexmo.com/v0.1/messages -H 'Authorization: Bearer elided' -H 'Content-Type: application/json' -H 'Accept: application/json' -d $'{ "from": { "type": "whatsapp", "number": "elided" }, "to": { "type": "whatsapp", "number": "elided" }, "message": { "content": { "type": "text", "text": "This is a WhatsApp Message sent with curl" } } }'

(copied the structure from here: https://github.com/Nexmo/nexmo-curl-code-snippets/blob/master/messages/whatsapp/send-text.sh)

I have tested this and it works (I used the actual JWT and phone numbers in my tests, but I've elided them from the above snippet). I've executed it on the command line, and I've used Run within Mathematica.

I would like to use the Mathematica functions intended for web communication. I tried using URLExecute with a HTTPRequest that looks like this:

HTTPRequest[
  "https://api.nexmo.com/v0.1/messages",
  <|
    Method->"POST",
    "Headers"->
      {
      "Authorization"->"Bearer "<>JWT,
      "Accept"->"application/json"
      },
    "ContentType"->"JSON",
    "Body"-> "{ "from":{ "type":"whatsapp", "number":"elided" }, "to":{ "type":"whatsapp", "number":"elided" }, "message":{ "content":{ "type":"text", "text":"This was sent from MMA" } } }"
  |>
]

This fails with a bad request error (400).

First question: Why is this HTTPRequest not a correct transformation of the curl command to a Mathematica expression?

Second question: Are there general guidelines for how to do such transformations correctly?

Third question: Does Mathematica offer any sort of inspection or debugging capabilities that would allow me to see what actual POST is generated by the URLExecute?

POSTED BY: Eric Rimbey
4 Replies

Glad to help.

The only other trick I had trouble with on URL's is handling usernames and passwords.

I found that you must put this in the body:

"Body" -> {"username" -> myusername, "password" -> mypassword}

Regards,

Neil

POSTED BY: Neil Singer

Eric,

From my experience, you need to put all the "-H" stuff in a header, and the "-d" stuff needs to be in the body. Also, Quotation marks in strings must be escaped by typing \" (I can't test this so this is a best guess):

HTTPRequest["https://api.nexmo.com/v0.1/messages",
 <|Method -> "POST",
  "Headers" -> {
    "Authorization" -> "Bearer " <> JWT ,
    "Content-Type" -> "application/json",
    "Accept" -> "application/json"}, 
  "Body" -> 
   "{ \"from\":{ \"type\":\"whatsapp\", \"number\":\"elided\" }, \"to\
\":{ \"type\":\"whatsapp\", \"number\":\"elided\" }, \"message\":{ \
\"content\":{ \"type\":\"text\", \"text\":\"This was sent from MMA\" \
} } }"|>]

Let me know if it works!

Regards,

Neil

POSTED BY: Neil Singer
Posted 4 years ago

Okay, I'll try moving content type to header argument. As for the escaped quote marks, they actually are escaped in my code, I just did a bad job of copy-pasting in my original post. But that is the kind of thing I would like to be able to inspect/debug--maybe something is malformed that I just can't see. Thanks!

POSTED BY: Eric Rimbey
Posted 4 years ago

Oh, brother. That was it! Moving the ContentType argument into the Header argument as "Content-Type" worked! Thank you so much!

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract