Problems with get request. Error 400

Problems with get request. Error 400.
I’m writing a ribbon with com port and send it to the server. The server gives 400 replies.

#include <SPI.h>
#include <HttpClient.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <stdio.h>
#include <string.h>

// Name of the server we want to connect to
const char kHostname = “10.2.10.253”;
// Path to download (this is the bit after the hostname in the URL
// that you want to download
const String kPath = “/arduino/hs/cominterface/post-data/”;
//const char kPath = “/zakazu_v3/hs/cominterface/post-data/”;
byte mac = { 0xDE, 0xAD, 0x7E, 0x7F, 0xFE, 0xED };

// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 5*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000;

const int MaxCountBuff = 1000;
// Массив для принимаемых байт
char InputDataBuffer[MaxCountBuff];

void setup()
{

// initialize serial communications at 9600 bps:
Serial.begin(115200);

while (Ethernet.begin(mac) != 1)
{
Serial.println(“Error getting IP address via DHCP, trying again…”);
delay(15000);
}
}

bool CheckCRC(char InputData, int Count){

char CRC;
// Складываем байты массива
for( int i = 0; i < Count; i++){
CRC = CRC + InputData*; *

  • }*

}
String inString = “”;
void loop()
{

  • if (Serial.available() > 0) {*

  • int inChar = Serial.read();*

  • inString += (char)inChar;*

  • if (inChar == ‘\n’) {*

  • Serial.println(inString);*

  • String url = kPath + inString; *

  • int length_ = url.length() + 1;*

  • char data_[length_];*

  • url.toCharArray(data_, length_);*

  • Serial.println(data_);*

  • sendServer(data_);*

  • }}*

  • //sendServer(kPath.);*

  • //memset(InputDataBuffer, 0, sizeof(InputDataBuffer));*

  • //}*

  • }*

void sendServer(char kPath_l[]){
int err =0;

  • EthernetClient c;*

  • HttpClient http(c);*

  • http.beginRequest();*

  • http.get(kHostname, kPath_l);*

  • // http.sendHeader(“Authorization”, “Basic Q09NIGludGVyZmFjZTo3RyomeU42Kj93Mko=”);*
    _ // http.sendBasicAuth(“COM interface”,“7G*&yN6*?w2J”);_

  • http.endRequest();*

  • if (err == 0)*

  • {*

  • Serial.println(“startedRequest ok”);*

  • err = http.responseStatusCode();*

  • if (err >= 0)*

  • {*

  • Serial.print("Got status code: ");*

  • Serial.println(err);*

  • // Usually you’d check that the response code is 200 or a*

  • // similar “success” code (200-299) before carrying on,*

  • // but we’ll print out whatever response we get*

  • err = http.skipResponseHeaders();*

  • if (err >= 0)*

  • {*

  • int bodyLen = http.contentLength();*

  • Serial.print("Content length is: ");*

  • Serial.println(bodyLen);*

  • Serial.println();*

  • Serial.println(“Body returned follows:”);*

  • // Now we’ve got to the body, so we can print it out*

  • unsigned long timeoutStart = millis();*

  • char c;*

  • // Whilst we haven’t timed out & haven’t reached the end of the body*

  • while ( (http.connected() || http.available()) &&*

  • ((millis() - timeoutStart) < kNetworkTimeout) )*

  • {*

  • if (http.available())*

  • {*

  • c = http.read();*

  • // Print out this character*

  • Serial.print(c);*

  • bodyLen–;*

  • // We read something, reset the timeout counter*

  • timeoutStart = millis();*

  • }*

  • else*

  • {*

  • // We haven’t got any data, so let’s pause to allow some to*

  • // arrive*

  • delay(kNetworkDelay);*

  • }*

  • }*

  • }*

  • else*

  • {*

  • Serial.print("Failed to skip response headers: ");*

  • Serial.println(err);*

  • }*

  • }*

  • else*

  • { *

  • Serial.print("Getting response failed: ");*

  • Serial.println(err);*

  • }*

  • }*

  • else*

  • {*

  • Serial.print("Connect failed: ");*

  • Serial.println(err);*

  • }*

  • inString = “”;*

  • http.stop();*

  • }*

  const String kPath = "/arduino/hs/cominterface/post-data/";

This is just a waste of resources. The ONLY excuse for using a String is that you don't understand how to use a NULL terminated char array, so you need the crutches. Declaring that instance to be const, so that it is non-modifiable just boggles the mind.

  int inChar = Serial.read();

Using a type (char) in the name of a variable, when the type in the name is not the type of the variable, shows a real lack of common sense.

      int length_ = url.length() + 1;
      char data_[length_];
      url.toCharArray(data_, length_);
      Serial.println(data_);
      sendServer(data_);

It is not necessary to make a copy of the data in the String instance.

      sendServer(url.c_str());
  const String kPath = "/arduino/hs/cominterface/post-data/";

I find it interesting that you are making a GET request that consists of unknown data tacked onto this path, with post in the name.

just what does the URL you are trying to get look like?

it works

void loop()
{
if (Serial.available() > 0) {
int inChar = Serial.read();
inString += (char)inChar;
inString.replace("-","");
//inString.replace(" ","");
if (inChar == '\n') {

Serial.println(inString);
Serial.println("making POST request");

client.beginRequest();
client.post("/zakazu_v3/hs/cominterface/post-data");
client.sendBasicAuth("COM interface", "7G*&yN6*?w2J");
//client.sendHeader("Authorization", "Basic Q09NIGludGVyZmFjZTo3RyomeU42Kj93Mko=");
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
client.sendHeader("Content-Length", inString.length());
client.sendHeader("X-Custom-Header", "custom-header-value");
client.beginBody();
client.print(inString);
client.endRequest();

int statusCode = client.responseStatusCode();
String response = client.responseBody();

client.stop();

Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);

delay(1000);
inString = "";

}
}

}

  int inChar = Serial.read();

Still using stupid names, I see.

That variable CLEARLY should be called inFloat.