NetTalk Central

Author Topic: Curl Command  (Read 240 times)

wasatchconsulting

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Curl Command
« on: October 19, 2021, 10:48:25 AM »
I am trying to post a curl statement for sending SMS messages and I it is not going through. They give the example cUrl statement as below.

curl -X "POST" "https://rest.nexmo.com/sms/json" \
  -d "from=18339143526" \
  -d "text=A text message sent using the Vonage SMS API" \
  -d "to=18015575583" \
  -d "api_key=74ab237f" \
  -d "api_secret=s1n2GPpsWr0QBd2M"

If I place it in an onlline cURL testing it works, but how would I enter using NetTalk?

Thanks
Ken Watts

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10609
    • View Profile
Re: Curl Command
« Reply #1 on: October 19, 2021, 07:09:27 PM »
in Curl, -d refers to data. It's the equivalent of a nettalk call to SetValue.
So this call translates to

 net.setvalue('from','18339143526')
 net.setvalue('text','A text message sent using the Vonage SMS API')
 net.setvalue('to','18015575583')
 net.setvalue('api_key','74ab237f')
 net.setvalue('api_secret','s1n2GPpsWr0QBd2M')
 net.Post('https://rest.nexmo.com/sms/json')

The -X is interesting - that changes the command from GET to POST, but explicitly does not actually "do" a regular POST.
So it will be interesting to see if my above code suggestion works or not. If not, then there's an additional step - but I'm hoping that's unnecessary.

Cheers
Bruce

wasatchconsulting

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: Curl Command
« Reply #2 on: October 20, 2021, 06:04:25 AM »
Bruce,

Thanks for the response. I coded it this way but the API came back with "unparsable JSON", so it looks like they want the data send in JSON format.

Thanks
Ken Watts

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 10609
    • View Profile
Re: Curl Command
« Reply #3 on: October 21, 2021, 10:50:59 PM »
ok, so there's some complexity here that is worth clearing up.

when you do a request to the server there are basically several moving parts. Let's see them as

VERB  URL?parameters
HEADERS

DATA

There are "norms" but every so often there are variations to the norm, and this is one of those cases.

In your CURL example it is doing a "normal" GET - but with the verb changed from GET to POST. So

POST URL?Parameters

_without_ a Data section.

Your API also accepts data in the data section, instead of the parameters (which is considered much better), but then the data is in JSON format, not parameter format.

NetTalk starts with the norms, and goes from there. So your code is failing because when you do a POST the norm is to put data into the data part, not the parameters part, so the API at the other end is complaining.

Fortunately getting data in as Json is easy - especially in this case;

  str.SetValue('{{"from":":"18339143526","text":"A text message sent using the Vonage SMS API", to":"18015575583","api_key":"74ab237f","api_secret":"s1n2GPpsWr0QBd2M"}')

  net.Post('https://rest.nexmo.com/sms/json',str)

For more complex Json I recommend jFiles.

Cheers
Bruce


wasatchconsulting

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: Curl Command
« Reply #4 on: October 22, 2021, 09:03:53 AM »
Thanks Bruce.

I tried the LIBCURL and it is working now using plain text. Seems like no matter the order in the JSON format, it kept saying I was missing the API key. Tried the plain text method using the libcurl library and it goes through. Not sure why they would not accept the JSON, it looked just fine. I actually copied your JSON code and it still rejected because it says the JSON was missing the API key.

Thanks for you help
Ken Watts

michelis

  • Hero Member
  • *****
  • Posts: 1748
    • MSN Messenger - alberto-michelis@hotmail.com
    • View Profile
    • ARMi software solutions
    • Email
Re: Curl Command
« Reply #5 on: October 22, 2021, 10:28:58 AM »
May be you copy the json and did not realized it has, may be, two typos
  str.SetValue('{{"from":":"18339143526","text":"A text message sent using the Vonage SMS API", to":"18015575583","api_key":"74ab237f","api_secret":"s1n2GPpsWr0QBd2M"}')
should be
  str.SetValue('{{"from":"18339143526","text":"A text message sent using the Vonage SMS API","to":"18015575583","api_key":"74ab237f","api_secret":"s1n2GPpsWr0QBd2M"}')
« Last Edit: October 22, 2021, 10:48:10 AM by michelis »
-----------
Regards
Alberto

wasatchconsulting

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Re: Curl Command
« Reply #6 on: October 24, 2021, 07:43:04 AM »
I actually ran across another post which showed the code for sending to this API.

      net.SetAllHeadersDefault()
      net.SetContentType('form')
      net.SetValue('api_key','xxxxxxxx')
      net.SetValue('api_secret','xxxxxxxxxxxxxxxx')
      st.SetValue(clip(pText))
      st.ToUnicode(st:EncodeUtf8,st:CP_WINDOWS_1258)

      net.SetValue('text',st.GetValue())
      net.SetValue('from','18339143526')
      net.SetValue('to',pNummer)
      net.Post('https://rest.nexmo.com/sms/json')

This worked. Thanks for the assistance.
Ken Watts