400 bad request

hello,
i’m trying to send DHT11 data to a website with this code

#include <EtherCard.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 5

#define key "energyWayArduino" // put your key here
#define ethCSpin 10 // imposta il pin per il CS dell'ethernet (io uso sempre il 10)
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };     //impostiamo il valore MAC del dispositivo (deve essere unico nella LAN)
const char website[] PROGMEM = "servizi.vilfredo.it/dataReceiverArduino/ard_data";
byte Ethernet::buffer[500];
uint32_t timer;
Stash stash;
byte session;

//impostiamo una variabile di tempo
int res = 100; 



void setup () {
  Serial.begin(9600);
  Serial.println("\n[inizio caricamento]");

  
  initialize_ethernet();      //inizializziamo la connessione con ethernet
}


void loop () { 

 int chk;
  chk = DHT.read(DHT11_PIN);    // READ DATA

 
  
  if (res > 220){     //se non riceviamo una risposta corretta facciamo ripartire la connessione
    initialize_ethernet(); 
  }
  
  res = res + 1;
  
  ether.packetLoop(ether.packetReceive());
  
  //200 res = 10 secondi (50ms ogni ciclo res)
  if (res == 200) {

    byte sd = stash.create();
    stash.print("field1=");
    stash.print((float)DHT.humidity);
    stash.print("&field2=");
    stash.print((float)DHT.temperature);
    stash.print("&unit_code=arduino01");
    stash.save();

    //stampiamo a monitor seriale i valori del DHT11
    Serial.println((float)DHT.humidity);
    Serial.println((float)DHT.temperature);


    //generiamo gli header che vengono stampati anche a monitor seriale
    Stash::prepare(PSTR("POST /update HTTP/1.1" "\r\n"
      "Host: $F" "\r\n"
      "Connection: close" "\r\n"
      "password: $F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),
    website, PSTR(key), stash.size(), sd);

    // spedisce il pacchetto tcp
    session = ether.tcpSend(); 

 
 int freeCount = stash.freeCount();
    if (freeCount <= 3) {   Stash::initMap(56); } 
  }
  
   const char* reply = ether.tcpReply(session);
   
   if (reply != 0) {
     res = 0;
     Serial.println(F(" >>>REPLY recieved...."));
     Serial.println(reply);
   }
   delay(300);
}



void initialize_ethernet(void){  
  for(;;){ 

    if (ether.begin(sizeof Ethernet::buffer, mymac, ethCSpin) == 0){ 
      Serial.println( "Failed to access Ethernet controller");
      continue;
    }
    
    if (!ether.dhcpSetup()){
      Serial.println("DHCP failed");
      continue;
    }

    ether.printIp("IP:  ", ether.myip);
    ether.printIp("GW:  ", ether.gwip);  
    ether.printIp("DNS: ", ether.dnsip);  

    if (!ether.dnsLookup(website))
      Serial.println("DNS failed");

    ether.printIp("SRV: ", ether.hisip);

    //fa ripartire il conteggio per il reset
    res = 180;
    break;
  }
}

but it doesn’t upload data and it sends me this error:

>>>REPLY recieved…
HTTP/1.1 400 Bad Request
Server: nginx
Date: Sat, 30 Apr 2016 07:05:41 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

400 Bad Request

400 Bad Request



nginx


i’m using the sketch that i used for Thingspeak, and with this website it works…
i’m using an arduino uno with enc28j60 ethernet module.
thank you for your support.

const char website[] PROGMEM = "servizi.vilfredo.it/dataReceiverArduino/ard_data";

Websites used as value in the Host: field don't have a path attached.

Thank you for your answer, how can i correct that issue? can you give me an example? i tried to send data with GET and it works, but i want to send them with POST method.

Thank you again.

jumbobosco: how can i correct that issue?

By moving the path to the POST, but that seems to be obvious.

jumbobosco: i tried to send data with GET and it works, but i want to send them with POST method.

You must do whatever makes you happy.

I'm sorry but i'm a beginner, can you modify the part of the code? Really thank you for you patience! :confused:

I could, but I will not.

'I'm a beginner' is no excuse for laziness, it's absolute basic stuff.

jumbobosco:
Thank you for your answer,
how can i correct that issue? can you give me an example?
i tried to send data with GET and it works, but i want to send them with POST method.

Thank you again.

Why POST?

It is more of a pain int the butt than GET.

Ok whandall, can you explain me which path should i attach to the host? If i'm asking you it is because i don't know how to do that.

jumbobosco: Ok whandall, can you explain me which path should i attach to the host?

As already stated, I think there should be no paths attached to host.

Your code

const char website[] PROGMEM = "servizi.vilfredo.it/dataReceiverArduino/ard_data";
    Stash::prepare(PSTR("POST /update HTTP/1.1" "\r\n"
      "Host: $F" "\r\n"
      "Connection: close" "\r\n"
      "password: $F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),

I think it could work better like this

const char website[] PROGMEM = "servizi.vilfredo.it";
    Stash::prepare(PSTR("POST /dataReceiverArduino/ard_data/update HTTP/1.1" "\r\n"
      "Host: $F" "\r\n"
      "Connection: close" "\r\n"
      "password: $F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),

Thank you whandall! In fact i saw many posts saying that in the website you have to put only the dominion of the site. But i didn't know what that was meaning, now i understand! I'll try that code as soon as possible. Thank you again!

this is my new error after your update:

HTTP/1.1 404 Not Found
Date: Mon, 02 May 2016 18:49:37 GMT
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1021
Vary: Accept-Encoding
Connection: close
Apache Tomcat/7.0.54 - Error report<!–H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-se

Can the error be in the web page? is it possible?

HTTP/1.1 404 Not Found

Now the path is wrong.

Thank you very much! i had to delete "/update" in my code, and now everything works fine! that's my reply:

HTTP/1.1 200 OK Date: Mon, 02 May 2016 19:36:39 GMT Server: Apache-Coyote/1.1 Content-Length: 0 Connection: close

but why Content-Lenght=0?

Apart from the header there is no more data from the server.

The php script (? or apache itsself) does not generates more.

BTW. what respose would you expect from a POST? "Thank you for posting?"

A gracious website should do that :) thank you for teaching me! you solved all my problems!