-->
Page 1 of 3

Unable to do HTTP POST on PLIVO using ESP8266

PostPosted: Sun Jul 31, 2016 1:51 am
by salmanali11053
Hey Guys,

I am working on a project in which I need to send one SMS if the Input pin is active(logic 1). I have to use PLIVO for sending sms and that sms can be sent by doing HTTP POST on the given URL by the plivo.

https://www.plivo.com/docs/api/message/

I am able to send HTTP POST on servers like thingspeak.com, experttexting.com and some http test servers but not on PLIVO Server.
I am able to send HTTP POST on PLIVO from web clients like POSTMAN, POSTER and HTTPRequester but not from the ESP8266 module.

Here is the raw transaction log from POSTMAN for successful SMS Sent.

Code: Select allPOST /v1/Account/MYAUTHID/Message/ HTTP/1.1
Host: api.plivo.com
Content-Type: application/json
Authorization: Basic TUFaSkRITldaSFlUS1dNVFZIWU06TXpSaU1XWm1OVGxrTmpGaE9XTmhOelJpWTJRMU5EaGlaR0ZqWVRKag==
Cache-Control: no-cache
Postman-Token: 02b0f9b7-234a-bc21-42c4-eb66c579a99d

{
  "src" : "+13303623231",
  "dst" : "+17133028183",
  "text" : "Hi from PLIVO"
}


This is a raw transaction log from HTTPRequester..
Code: Select allPOST https://api.plivo.com/v1/Account/MYAUTHID/Message/
Authorization: Basic
Content-Type: application/json
Username: MYUSERNAME
{
  "src" : "+13303623231",
  "dst" : "+17133028183",
  "text" : "Hi from PLIVO"
}


This is how I am posting from my ESP8266..

After connecting to the WiFi router..

Code: Select allconst char * host = "api.plivo.com";

void loop(void)
{
  /* Check if module is connected to the Server */
  if (client.connect(host, 80))
  {
         Serial.print("Connected to ");
         Serial.println(host);

        SendSMS();
        CheckServerResponse();
  }
}

void SendSMS(void)
{   
    String body = "{\n\"src\" : \"+13303623231\",\n\"dst\" : \"+17133028183\",\n\"text\" : \"Input 1 Triggered,   Alert from Salman via PLIVO on 31/07/2016\"\n}";
 
     client.print("POST https://api.plivo.com/v1/Account/MYAUTID/Message/");
     client.print("Authorization: Basic\n");
     client.print("Content-Type: application/json\n");
     client.print("Username: MYAUTHID\n");
     client.print("\n");
     client.print(body);
}

void CheckServerResponse(void)
{
  Serial.println("Server Response....");
 
  /* Read all the lines of the reply from server and print them to Serial */
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
}



I am not getting any response from the server. Am I missing something? Please help guys..

Re: Unable to do HTTP POST on PLIVO using ESP8266

PostPosted: Sun Jul 31, 2016 10:25 am
by martinayotte
Few thing I see :
You are using unsecured HTTP client, while it seems that PLIVO is using a secured "https".
That can become a big issue.
But if PLIVO allows using unsecured request, like your POSTMAN example seems to details, then you can try to remove the URL portion in your POST, code would be like :
Code: Select allclient.print("POST /v1/Account/MYAUTID/Message/");

But, you will also need to add proper token like your POSTMAN example have :
Code: Select allclient.print("Authorization: Basic TUFaSkRITldaSFlUS1dNVFZIWU06TXpSaU1XWm1OVGxrTmpGaE9XTmhOelJpWTJRMU5EaGlaR0ZqWVRKag==");

In other words, make sure the request is identical to the one send by POSTMAN.

Re: Unable to do HTTP POST on PLIVO using ESP8266

PostPosted: Mon Aug 01, 2016 5:53 am
by salmanali11053
martinayotte wrote:Few thing I see :
You are using unsecured HTTP client, while it seems that PLIVO is using a secured "https".
That can become a big issue.
But if PLIVO allows using unsecured request, like your POSTMAN example seems to details, then you can try to remove the URL portion in your POST, code would be like :
Code: Select allclient.print("POST /v1/Account/MYAUTID/Message/");

But, you will also need to add proper token like your POSTMAN example have :
Code: Select allclient.print("Authorization: Basic TUFaSkRITldaSFlUS1dNVFZIWU06TXpSaU1XWm1OVGxrTmpGaE9XTmhOelJpWTJRMU5EaGlaR0ZqWVRKag==");

In other words, make sure the request is identical to the one send by POSTMAN.


Hey I've tried by sending the String similar to POSTMAN log too, but no response from the server :(

Re: Unable to do HTTP POST on PLIVO using ESP8266

PostPosted: Mon Aug 01, 2016 2:52 pm
by bbx10node
Code: Select all     client.print("POST https://api.plivo.com/v1/Account/MYAUTID/Message/");
     client.print("Authorization: Basic\n");


The first line does not end with newline.

If adding a newline does not work, I would start capturing packets using tcpdump or wireshark to see what is really going on. I use a Linux firewall so it is possible to capture packets going to/from the Internet. I do know how you can do this using a regular WiFi router.