I am trying to send a Post request from my Arduino Mega using the Ethernet shield, I tried already many many codes all over the internet but I haven't done yet
Also did it already from a NodeMCU-ESP8266 but I don't know why with the mega is getting so tricky
From this code everything goes well except that I never get the POST request, I am using this website 'requestcatcher' to test the POST request
#include <Ethernet.h>
#include <SPI.h>
// Conf. mac
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Server to Post
char server[] = "http://abc.requestcatcher.com/test";
// Starting Ethernet client
EthernetClient client;
// =============== Connecting to internet =============== //
void setup() {
// Open serial communications and wait for port to open:
// wait for serial port to connect. Needed for native USB port only
Serial.begin(9600);
while (!Serial) {
;
}
// Connecting to internet
if (Ethernet.begin (mac) == 0) {
Serial.println("Can’t connect via DHCP");
}
// Give the Ethernet shield a second to initialize
delay(1000);
// Printing the IP Adress
Serial.print ("IP Address: ");
Serial.println(Ethernet.localIP());
}
/////============= Sending Post request ============= ////
void loop() {
Serial.println(" - Post request in process - ");
if (client.connect(server, 80) {
Serial.print(" Sending Post request ");
client.println("POST /test HTTP/1.1");
client.println("Host: abc.requestcatcher.com/");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: ");
client.println();
}
else {
Serial.println("Can’t reach the server");
}
// Wait 10 secs
delay(10000);
}
Arduino prints via serial something like this
IP Adress: 192.168.100.40
- Post request in process -
Sending Post request
- Post request in process -
Sending Post request
- Post request in process -
Sending Post request
So I think that means that the Arduino connects successfully to the internet and also 'client.connect(server, 80)' goes true since it prints 'Sending Post request', but I don't know why request catcher never gets any of the post requests, I tested 'requestcatcher' with online apps and as well with the NodeMCU and it gets the post request from all except the Arduino so I think something must be wrong around here:
client.println("POST /test HTTP/1.1");
client.println("Host: abc.requestcatcher.com/");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: ");
client.println();
Please help, any hint would be very helpful