http request for API control

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.

May be a long shot, but have you tried without the http part in your host name string, like so

String PATH_NAME = "192.168.0.11:1400/api_test.html";

Its been a while since i did this with an arduino, but figured id offer something to try.
Also, how far into the code does it work? I see alot of serial print, does it ever fail at a certain point?
If its failing and not sending anything to the prints you have setup, add a few more in the initialization portion to see if that is working and so on.

I tried it without the "http://" but it still doesn't work.

When I put a serial.printl after the http request I see this on the serial monitor, so it is definitely sent, but not correctly.

Since the request is in the setup, it doesn't really matter what's happening in the loop. Is that correrct?

did you mean to post what you were seeing and didnt?

If you can post what you are getting in return that doesnt look correct and we can try to help.

Also, something to look into is the Serial.begin(9600); line, do the SPI and Ethernet call for 9600?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.