0

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.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Jan 3, 2017 at 18:29
7
  • 1
    You should output the contents of HTTP_req to see where/if json is in it. Commented Jan 3, 2017 at 18:55
  • If I run URL in browser without json text in it then how from where json will come on HTTP_req Commented Jan 3, 2017 at 19:26
  • @Musa even if I do not pass json it returns json HTTP_req GET /json HTTP/1.1 Host: 192.168.1.177:8090 U Commented Jan 4, 2017 at 17:00
  • I see a json in there, you just check the whole thing you should be only checking the url parameters Commented Jan 4, 2017 at 17:05
  • even if I do not pass json in the url parameters i still get json in HTTP_req Commented Jan 4, 2017 at 17:06

1 Answer 1

0

So figured out the problem, the issue was I was closing the connection instead when it was json fetched, I had to keep alive the connection.

so replaced client.println("Connnection: close"); with client.println("Connection: keep-alive");

which fixed the problem.

answered Jan 4, 2017 at 18:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.