Sending a POST request
Posted: Tue Mar 08, 2016 4:24 pm
Helle everyone,
right now I am trying to send a push message to my windows phone.
I am using the pushalot (http://www.pushalot.com) service to send messages to my smartphone.
You can send a simple POST request to the homepage and it forwards your information to your smartphone. The necessary information included in the request is shown below (also described here http://www.pushalot.com/api):
AuthorizationToken: XXXXXXXXXXXXXXXXXXXXXX
Body: "this is an example message"
I wrote a really simple html-file with the following code:
This works perfectly. With the ESP8266 I wasn't successful until now. Here is the Code:
It seems that I can connect to the website but the post request doesn't work. Can someone of you help me out and tell me what is wrong with my code.
Thank you very much in advance for every hint.
Greetings Daniel
right now I am trying to send a push message to my windows phone.
I am using the pushalot (http://www.pushalot.com) service to send messages to my smartphone.
You can send a simple POST request to the homepage and it forwards your information to your smartphone. The necessary information included in the request is shown below (also described here http://www.pushalot.com/api):
AuthorizationToken: XXXXXXXXXXXXXXXXXXXXXX
Body: "this is an example message"
I wrote a really simple html-file with the following code:
Code: Select all
<html>
<body>
<FORM action="https://pushalot.com/api/sendmessage" method="POST">
<P>
<LABEL for="firstname">Data To Send:</LABEL>
<INPUT type="text" name="AuthorizationToken" value="XXXXXXXXXXXXXXXXXXXXXX" readonly><BR>
<INPUT type="text" name="Body"><BR>
<INPUT type="submit" value="Submit">
</P>
</FORM>
</body>
</html>
This works perfectly. With the ESP8266 I wasn't successful until now. Here is the Code:
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
// data Accesspoint
const char* ssid = "ESP";
const char* password = "";
// remote site information
const char http_site[] = "pushalot.com";
const int http_port = 80;
// network
char ssids[] = "SSID"; // ssid
char passwords[17] = "Password"; // pass
IPAddress ip(192,168,178,38);
IPAddress gateway(192,168,178,1);
IPAddress subnet(255,255,255,0);
// global variables
WiFiClient client;
void WiFiconnect() {
Serial.begin(9600);
Serial.println();
Serial.println("Starting WiFi...");
WiFi.mode(WIFI_STA); //STA
WiFi.begin(ssids, passwords);
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
WiFi.config(ip,gateway,subnet);
Serial.println("WiFi successfully connected...");
Serial.print("Connected to ");
Serial.println(ssids);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
// WiFi Setup
Serial.println("Stationmode failed!");
Serial.println("Starting Accesspoint...");
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid,password);
WiFi.softAPConfig(ip, gateway, subnet);
// establish connection
Serial.printf("accesspoint with the name ");
Serial.println(ssid);
Serial.print("IP-Address: ");
Serial.println(WiFi.softAPIP());
Serial.print("Mac-Address: "); // show mac address
Serial.println(WiFi.softAPmacAddress());
}
}
bool getPage() {
// connect with remote server
if ( !client.connect(http_site, http_port) ) {
return false;
}
// HTTP POST request
client.println("POST /api/sendmessage HTTP/1.0");
client.print("Host: ");
client.println(http_site);
client.print("Content-Type: ");
client.println("application/x-www-form-urlencoded");
client.print("AuthorizationToken: ");
client.println("XXXXXXXXXXXXXXXXXXXXXX");
client.print("Body: ");
client.println("this is an example message");
client.println("Connection: close");
client.println();
return true;
}
void setup()
{
WiFiconnect();
if ( !getPage() ) {
Serial.println("GET request failed");
}
else {
Serial.println("GET request successful");
}
}
void loop()
{
}
It seems that I can connect to the website but the post request doesn't work. Can someone of you help me out and tell me what is wrong with my code.
Thank you very much in advance for every hint.
Greetings Daniel