Error 400 "Bad request"


#1

Hi there.
This code has been working fine on my Adafruit Fona and then all of a sudden I get Error 400. I must be missing something here, Ive read and reread the API documents and I just cant see my problem.

Code below:

String varString = "{“Volume_1”: " + String(vol1,2)+ ", “Volume_2”: " + String(vol2,2)+ ", “Average”: " + String(ave,2)+ “, “Error”: " + Error + “, “Signal”: " + String(csq)+ “}” ; //build string for upload
int num = varString.length();
SerialMon.println(“Sending String:”);
SerialMon.println(varString);
SerialMon.print(“With Length :”);
SerialMon.println(num);
SerialMon.print(F(“Connecting to “));
SerialMon.print(server);
SerialMon.println(“Please wait”);
if (client.connect(server,port)) {
client.print(F(“POST /api/v1.6/devices/”));
client.print(deviceLabel);
client.print(F(” HTTP/1.1\r\n”));
client.print(F(“Host: “));
client.print(server);
client.print(F(”\r\n”));
client.print(F(“X-Auth-Token: “));
client.print(token);
client.print(F(”\r\n”));
client.print(F(“Connection: close\r\n”));
client.print(F(“Content-Type: application/json\r\n”));
client.print(F(“Content-Length: “));
client.print(num);
client.print(F(”\r\n\r\n”));
client.print(varString);
client.print(F(”\r\n”));

SerialMon.print(F("Making request to Ubidots:\n"));
SerialMon.print("POST /api/v1.6/devices/");
SerialMon.print(deviceLabel);
SerialMon.print(" HTTP/1.1\r\n");
SerialMon.print("Host: ");
SerialMon.print(server);
SerialMon.print("\r\n");
SerialMon.print("X-Auth-Token: ");
SerialMon.print(token);
SerialMon.print("\r\n");
SerialMon.print("Connection: close\r\n");
SerialMon.print("Content-Type: application/json\r\n");
SerialMon.print("Content-Length: ");
SerialMon.print(num);
SerialMon.print("\r\n\r\n");
SerialMon.print(varString);
SerialMon.print("\r\n");

}

Thanks so much for your help!
Cheers
Matt


#2

I seemed to have solved this by building a char array, but first converting all the variables from float to char array too.

char chvol1[8], chvol2[8], chave[8],chcsq[3];
int csq = modem.getSignalQuality();
dtostrf(csq, 2, 0, chcsq);
dtostrf(vol1, 7, 2, chvol1); //convery values from float to char array
dtostrf(vol2, 7, 2, chvol2);
dtostrf(ave, 7, 2, chave);
int str_len = Error.length() + 1; //get length of error string
char cherror[str_len];
Error.toCharArray(cherror, str_len);
sprintf(varString,"{“Volume_1”: %s, “Volume_2”: %s, “Average”: %s, “Error”: %s,“Signal”: %s}",chvol1,chvol2,chave,cherror, chcsq);
num = strlen(varString); //return payload length


#3

OK, so the above code worked for a while, now it no longer does. What am I missing? Still getting an Error 400.
Here is my request structure:

POST /api/v1.6/devices/Complete_Setup_Test HTTP/1.1
Host: industrial.api.ubidots.com
X-Auth-Token: *******
Connection: close
Content-Type: application/json
Content-Length: 87

{“Volume_1”: 2375.69, “Volume_2”: 2370.30, “Average”: 2372.99, “Error”: 0,“Signal”: 16}

And the Response:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Tue, 02 Jul 2019 17:33:06 GMT
Content-Type: text/html
Content-Length: 166
Connection: close


#4

Greetings, you are using a non-valid quotes, this quote “ should be replaced by the standard one ", so your payload should look like the one below:

{"Volume_1": 2375.69, "Volume_2": 2370.30, "Average": 2372.99, "Error": 0,"Signal": 16}

All the best