Hello,
I'm trying to send a http request to a webserver to control an API, but it seems not to work. Here is my code:
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ethernet-shield-2
*/
#include <SPI.h>
#include <Ethernet.h>
// replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change the IP address, subnet mask, gateway's IP address, and DNS server's IP address depending on your network
IPAddress ip(192, 168, 0, 5);
IPAddress gateway(192, 168, 0, 0);
IPAddress subnet(255, 0, 0, 0);
IPAddress myDns(8, 8, 8, 8);
EthernetClient client;
int HTTP_PORT = 1400;
String HTTP_METHOD = "POST";
char HOST_NAME[] = "192.168.0.11";
String PATH_NAME = "http://192.168.0.11:1400/api_test.html";
String command = "SOME COMMAND";
void setup() {
Serial.begin(9600);
// initialize the Ethernet shield using the static IP address:
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// connect to web server on port 80:
if(client.connect(HOST_NAME, HTTP_PORT)) {
// if connected:
Serial.println("Connected to server");
delay(5000);
Serial.println(HTTP_METHOD + " " + PATH_NAME);
Serial.println("Content-Type: text/xml; encoding='utf-8'");
Serial.println("Host: " + String(HOST_NAME));
Serial.println("Content-Length: 137");
Serial.println("Expect: 100-continue");
Serial.println(command);
Serial.println("Connection: close");
Serial.println(); // end HTTP header
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME);
client.println("Content-Type: text/xml; encoding='utf-8'");
client.println("Host: " + String(HOST_NAME));
client.println("Content-Length: 137");
client.println("Expect: 100-continue");
client.println(command);
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void loop() {
// optional, check link status
if (Ethernet.linkStatus() == LinkON)
Serial.println("Link status: On");
else
Serial.println("Link status: Off");
delay(1000);
}
As you can see I used a tutorial and just modified it a bit. I looked at some tutorials but that was the one which made the most sense to me for my application.
I come as far as "connected to server".
I'm not sure if the header is really sent. And I also don't get any response.
When I use exactly these lines in a test tool from the webserver, it works. And it also works if I do it in html/java script.
So I think there is something wrong with the code.
Here are the lines the API test tool gives me when sending it to the server:
POST http://192.168.0.11:1400/api_test.html
Content-Type: text/xml; encoding='utf-8'
Host: 192.168.0.11:1400
Content-Length: 137
Expect: 100-continue
The Protocol is HTTP/TCP
And here is the java script function which aslo works:
function apitest(command, resultFunc)
{
var request = new XMLHttpRequest();
request.open('POST', 'http://192.168.0.11:1400/api_test.html', true);
request.setRequestHeader("Content-type", "text/xml; encoding='utf-8'");
request.send(command);
request.onload = function ()
{
resultFunc(request.status, request.responseText);
};
I hope you can help me with this.