using indexOf() and substring().

i would like to capture the value of a text box submitted from a form. My URL when the form is submitted is:

http://192.168.0.25/?startTime=33&duration=

GET /?startTime=33&duration= HTTP/1.1
Host: 192.168.0.25
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14
Accept-Language: en-us
Referer: http://192.168.0.25/?startTime=&duration=
Accept-Encoding: gzip, deflate

start time is=33&duration=

I need to set the variable startTime to 33.

I have the index of “startTime” in the URL. I can get what comes after “startTime” in the URL but I am getting everything that comes after it, not just the two number. (33).

My code is:

                        int stTimeInd = HTTP_req.indexOf("startTime=");
                      
                    
                        String startTime = HTTP_req.substring(stTimeInd+9);  ////////////   this is where i need the help (I think)
                        Serial.print("start time is");
                        Serial.println(startTime);

I commented the line where i think i am in trouble.

If indexOf() successfully returns the location of the =, why does it not successfully return the location of the &?

Or perhaps I should ask why you aren't asking for the location of the &. The substring of interest is between the = and the &, isn't it?

Im not sure what you are asking me. I am looking for the location of "startTime=" and what i want to capture is everything between "startTime=" and "&". So in other words using the example i gave in my original post, When i submit the form (URL: http://192.168.0.25/?startTime=33&duration=) the result of "Serial.println(startTime)" needs to be"start time is=33. What i am getting is :start time is=33&duration=

The line where i think i have the issue is:

String startTime = HTTP_req.substring(stTimeInd+9);

This is requesting a string starting at the 9th position after startTime=, what i need to find out is how to tell it where to stop. I want to limit what it returns to what is between "startTime=" and the ampersand.

Even if i could limit what it returns to 2 characters that would be fine as well since this will always be a two digit number.

I think the general approach would be to split the URL at the first '?' to find the start of the URL-encoded values, and then split the right hand side at '&' to separate the value assignments, and then split each assignment at '=' to separate the name from the value. If your URL can include entity references then the decoding would need to be a bit cleverer. This feels like the sort of common requirement that somebody somewhere will surely have implemented as a library.

If you only want to deal with this specific value then you could search for "startTime=" and the following '&' and anything in between them should be the value you want.

That is correct. The issue i have is how do i do it? What is the code i need to limit the string to what is between startTime= and the ampersand? I can get everything after startTime=, but how do i stop it at the ampersand?

Find the position of the ampersand. The string you want ends at the preceding character. You have already found where the string starts.

Im not sure what you are asking me. I am looking for the location of "startTime=" and what i want to capture is everything between "startTime=" and "&".

No, the string you want to capture is between the = and the &. You need to find the location of startTIme= ONLY so that you know that the substring between the = and the & IS the start time.

Once you know that indexOf("startTime=") is not -1, the the actual value does NOT matter.

Get the indexOf() the =. Get the indexOf() the &. The stubstring starts at indexOf("=")+. The length is indexOf("&") - indexOf("=) - 1.

NOW I GOT IT!!!!

Thanks for the help.

hi can u please send the code of the instruction because i have the same problem

+IPD,0,121:GET /?|Bonjour$ HTTP/1.1
Host: 192.168.1.30
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.0.1

i want to extract bonjour thank you .

hello Houssem99 .. i have the same issue.
would u please give me a clear line of indexof() .. so i capture my missing word from the http line?
thank you