简体   繁体   English

使用正则表达式从服务器回复中删除 HTML header

[英]Remove HTML header from server reply using Regular Expressions

I have an ESP32 T-CALL with an integrated GSM-unit and used this as a base in Arduino IDE.我有一个带有集成 GSM 单元的 ESP32 T-CALL,并将用作 Arduino IDE 的基础。

My code makes a server-call and processes the result.我的代码进行服务器调用并处理结果。 This code reads from the buffer and outputs it to a String.此代码从缓冲区读取并将其输出到字符串。 It uses a manually generated POST header, sent serially.它使用手动生成的 POST header,串行发送。 However, I need to remove the HTTP header, leaving only the JSON.但是,我需要删除 HTTP header,只留下 JSON。

while (client.connected() && millis() - timeout < 10000L) {
  // Print available data (HTTP response from server)
  while (client.available()) {
    char c = client.read();
    returnString += c;
    timeout = millis();
  }
}

The output comes with a complete header, like this: output 带有完整的 header,如下所示:

HTTP/1.1 200 OK
Content-Type: application/json
Server: Microsoft-IIS/10.0
X-Powered-By: PHP/8.0.0
X-Powered-By: ASP.NET
Date: Tue, 25 Jan 2022 00:12:31 GMT
Connection: close
Content-Length: 23

{"status:":"code6"}

I used the <regexp.h> library by Nick Gammon and the Lua-reference here in order to filter out everything to tle left of the curlybrace - however, I can't seem to get it right.我使用了 Nick Gammon 的<regexp.h>库和这里的 Lua-reference来过滤掉花括号左侧的所有内容 - 但是,我似乎无法正确处理。 I figure, something like this:我想,是这样的:

char result = ms.Match ("{(%x+)"); // Find the first curlybrace and only include this and everything to the right.

Alas, using this RegEx, no match is found.唉,使用这个 RegEx,找不到匹配项。 I also tried splitting at \r\n\r\n, using the getValue-function here but couldn't get it to accept a double linebreak.我还尝试在 \r\n\r\n 处拆分,在这里使用 getValue 函数,但无法让它接受双换行符。

Any ideas on, how to remove the header, using RegEx?关于如何使用 RegEx 删除 header 的任何想法?

This is not a direct answer on how to use the regex, however, if you want to skip the headers and get the payload, other than using regex, or a httpclient library that I suggested in the comment, it is not difficult to do that without using any library.这不是关于如何使用正则表达式的直接答案,但是,如果您想跳过标头并获取有效负载,而不是使用正则表达式或我在评论中建议的 httpclient 库,那么做到这一点并不难不使用任何库。

To skip the header and get the payload, you need to modify your code to find the end of the header.要跳过 header 并获取有效负载,您需要修改代码以找到 header 的结尾。


// skip the http headers
while (client.connected()) {
  String line = client.readStringUntil('\n');
  if (line == '\r') break;    //if line only contain '\r', it's the end of headers
  }
}

// get the payload
String payload;
while (client.available()) {
  payload = client.readStringUntil('\n');
}

You can then using a JSON library to extract the data out from the JSON object.然后,您可以使用 JSON 库从 JSON object 中提取数据。 Or for the simple JSON object as you shown, you can do it without a library.或者对于如图所示的简单 JSON object,您可以在没有库的情况下执行此操作。

payload.trim();                  // remove the '\r\n' at the end
payload.replace("status:", "");  // replace "status:" with ""
payload.replace("\"", "");       // remove all the "\""
payload.trim();
Serial.println(payload);

This will print out the value of code6 in your JSON object.这将打印出code6中 code6 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM