Problem with the interaction of an http server with Arduino and Axios js

Asked

Viewed 83 times

0

I am creating an http server on Arduino, which given a request it responds to the following:

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); 
client.println("Refresh: 5");  

client.println();

client.println("<!DOCTYPE HTML>");

client.println("<html>");
timeNow = millis()/60000; 
String times = String(timeNow, DEC);

String response = "{\n\"Response\": {\"workTime\":\"";
  response += times;
  response += "\"},\n";

response += "\"status\": \"200\"\n}";

client.println(response);
client.println("<br />");

client.println("</html>");

in the browser get this:

o que aparece na tela

Aba network

Chrome headers:

Request URL: http://1**.**.**.202:***0/resetRelogio
Request Method: GET
Status Code: 200 OK
Remote Address: 1**.**.**.202:***0
Referrer Policy: no-referrer-when-downgrade
Connection: close
Content-Type: text/html
Refresh: 5
Accept: 
 text/html, 
 application/xhtml+xml, 
 application/xml;q=0.9,
 image/webp,
 image/apng,*/*;q=0.8,
 application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7
Cache-Control: max-age=0
Connection: keep-alive
Cookie: qrClose=true; JSESSIONID=4db49dbd10380e82eb364e4fcd9636
Host: 1**.**.**.202:***0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36

aba response

In my test with Xios I have this:

const axios = require('axios')


axios.get('http://172.40.24.202:4000/resetRelogio')

   .then(function (response) {
       console.log(response);
    })

    .catch(function (error) {

        console.log("error");
        console.log(error);
    })

as an answer we have:

resposta console

I’ve searched a lot on google, so I understood the Rduino closes the connection unexpectedly, but I send a

client.println("Connection: close"); 

that the browser should understand the answer and close the connection. I needed Xios to return the body data.

  • 1

    And there is a /n after the \"200\" that is wrong.

  • Response += ""status": "200"/n}"; here is part of my reply body, is a json the status code is in the beginning. correct me if I’m wrong

  • 1

    But this /n shouldn’t be \n, which is a line break?

  • yes, it was a typo. Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.