I am trying to return JSON data if JSON is found in the HTTP header, and if not I am trying to display a normal page. But instead it always displays the JSON text, even if I do not pass the JSON in the URL! Full code is here.
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
// client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
if (!sentHeader) {
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
}
// Ajax request - send JSON output
if (util::StrContains(HTTP_req, "json")) {
// Spit out JSON data
client.println("Content-Type: application/json;charset=utf-8");
client.println("Server: ArduinoMega");
client.println("Connnection: close");
client.println();
client.print("{\"arduino\":[{\"location\":\"outdoor\",\"celsius\":\"");
client.print(tempInC);
client.print("\"}]}");
client.println();
break;
} else {
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close")); // the connection will be closed after completion of the response
client.println(F("Refresh: 1000")); // refresh the page automatically every 60 sec
client.println();
sentHeader = true;
client.println(F("<!DOCTYPE HTML>"));
client.println(F("<html><head><title>"));
client.println(F("Welcome to Arduino WebServer</title>"));
util::printProgStr(client, htmlStyleMultiline );
client.println(F("</head><body style='background-color:grey'>"));
client.println(c);
client.print(F("<p style='color:red';style='font-family: Arial'> LIVE: </p>"));
Please help.
HTTP_req
– Ciasto piekarz Jan 3 '17 at 19:26json
it returnsjson
HTTP_req GET /json HTTP/1.1 Host: 192.168.1.177:8090 U
– Ciasto piekarz Jan 4 '17 at 17:00json
in there, you just check the whole thing you should be only checking the url parameters – Musa Jan 4 '17 at 17:05HTTP_req
– Ciasto piekarz Jan 4 '17 at 17:06