我正在wifi微控制器上创建一个API。网站发出请求,并且微控制器(ESP8266)应该响应JSON数据。
我对HTTP响应有奇怪的问题:
网站发出第一个请求时,会收到空白响应
ESP8266发送主页时此HTTP响应没有问题:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html; charset=UTF-8");
client.println("");
client.println("<!DOCTYPE HTML>");
此代码出现问题:
// read incoming request
String request = client.readStringUntil('\r\n');
client.flush();
// read the request
if (request.length() > 15) {
// check if request is an api call and not a favicon call
if (request.indexOf("favicon") < 0) {
rqlength = request.length() - 15 ;
request = request.substring(5);
rqs = "";
rq["title"] = "request";
rq["date"] = request.substring(0,8);
rq["hour"] = String(timeClient.getHours());
serializeJson(rq, rqs);
// check if request is an api call and not a favicon request
// send the request to arduino
Serial.print(rqs);
// wait the response from the arduino
while (Serial.available()) {
incdata = Serial.readString();
cansend = true;
}
// send the arduino JSON data to the client : The issue seems to come from here ?
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Connection: close"));
client.println("");
client.print(incdata);
}
该问题似乎是由于缓存或http标头而发生的? 但是我找不到解决方案。 有什么想法吗?