0

I'm trying to extract POST fields data from this:

POST / HTTP/1.1
Host: 192.168.4.1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:85.0) Gecko/20100101 Firefox/85.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 54
Origin: http://192.168.4.1
Connection: keep-alive
Referer: http://192.168.4.1/
Upgrade-Insecure-Requests: 1

readHere=ok&ssidName=Itziks+Home&ssidPassword=123456789

The variable request hold the whole message:

WiFiClient client = server.available();
String request = client.readString();
Serial.println(request);

WifiClient documentation: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/client-class.html

I've tried to split it with strtok() like this and I get an convert error:

WiFiClient client = server.available();
String request = client.readString();
String req = strtok(request,'\r');
Serial.println(req);

And this is the error:

cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'

I can't really think of a practical way to doing this. Thank for the help.

Itzik.B
  • 1,023
  • 2
  • 15
  • 35
  • What is `client`? The class type of that is likely to have methods to help parse the post. Please provide a [complete minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – kaylum Feb 22 '21 at 20:29
  • Do you know how to extract anything from a string? How to "tokenize" a string? What research did you do? The `readhere=ok` starts after an empty line - maybe start with finding an empty line in a string first - POST values will be on the next line, right?? – KamilCuk Feb 22 '21 at 20:30
  • @kaylum, I've added the details about client class please refresh, thanks! – Itzik.B Feb 22 '21 at 20:37
  • @KamilCuk, I've tried with `str_tok` and `str_sep` and I reciving casting error: `cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'` – Itzik.B Feb 22 '21 at 20:39
  • `String` is not the same as `char *`. Call `c_str()` to convert String to a C string. `char *requestString = request.c_str();` Then you can use `strtrok` and other C string functions. – kaylum Feb 22 '21 at 20:51
  • You might want to check [this library](https://github.com/NatanBiesmans/Arduino-POST-HTTP-Parser) out - it looks like it does exactly what you're asking for. – John Bode Feb 22 '21 at 20:55

0 Answers0