guild icon
Toit
#Problem with changing pages in HTML handling ..
Thread channel in help
bmentink
bmentink 08/30/2023 09:13 PM
In the following http.Request handler I am trying to grab a number that the user types in a form page /start.html store it away, then force a page change back to index.html The code works apart from the last two lines, it won't change to the index page .. Handler is:
handle request/http.Request writer/http.ResponseWriter -> none: query := url.QueryString.parse request.path resource := query.resource if resource == "/": if bucket["element"]: resource = "index.html" else: resource = "start.html" // 1st time power up, throw up the start.html page if resource == "/start": resource = "start.html" if resource == "/extended": resource = "extended.html" if resource.starts_with "/": resource = resource[1..] TEMPORARY_REDIRECTS.get resource --if_present=: writer.headers.set "Location" it writer.write_headers 302 return result := RESOURCE_MAP.get resource --if_absent=: writer.headers.set "Content-Type" "text/plain" writer.write_headers 404 writer.write "Not found: $resource" return if result is string: result = result.substitute: look_up_variable it writer.headers.set "Content-Type" (mime_type resource) if compression_type resource: writer.headers.set "Content-Encoding" (compression_type resource) writer.write result if query.parameters.is_empty: return Element := query.parameters["Element"] element-size := int.parse Element bucket["element"] = element-size // Save new element size to flash bucket. writer.headers.set "Location" "index.html" writer.write_headers 302
floitsch
floitsch 08/30/2023 09:19 PM
This night be http thing, but I don't think you can set the content type header and then redirect.
I would expect that a redirect must be on its own. No body, or other headers.
bmentink
bmentink 08/30/2023 09:44 PM
What would be the correct way to do this, I know nothing re HTML :🙂:(edited)
floitsch
floitsch 08/30/2023 09:49 PM
Do I understand correctly that you want to send a resource to the user (basically a download) and then, when that's finished redirect to another page?
bmentink
bmentink 08/30/2023 09:55 PM
No, just present a form for the user to fill in (initially during commision), then when that is submitted, change to the normal index.html page. Currently my hack is to reset .. but would like to do it a bit better ..
floitsch
floitsch 08/30/2023 10:03 PM
I don't understand the code: you are writing something as a response (writer.write result) but then want to redirect.
Why do you write something if you just want to redirect?
bmentink
bmentink 08/30/2023 10:06 PM
The write is for pages that have variables ... maybe I need to do my redirect before it gets to there ..
floitsch
floitsch 08/30/2023 10:07 PM
That's probably it. If you have to redirect do it before writing anything as response.
floitsch
floitsch 08/30/2023 10:07 PM
And the return eagerly
bmentink
bmentink 08/30/2023 10:08 PM
Yep, thanks will try that out shortly ..
👍1
bmentink
bmentink 08/30/2023 10:21 PM
@floitsch It works ... see below:
bmentink
bmentink 08/30/2023 10:22 PM
handle request/http.Request writer/http.ResponseWriter -> none: query := url.QueryString.parse request.path resource := query.resource if resource == "/": if bucket["element"]: resource = "index.html" else: resource = "start.html" // 1st time power up, throw up the start.html page if resource == "/start": resource = "start.html" if resource == "/extended": resource = "extended.html" if resource.starts_with "/": resource = resource[1..] TEMPORARY_REDIRECTS.get resource --if_present=: writer.headers.set "Location" it writer.write_headers 302 return result := RESOURCE_MAP.get resource --if_absent=: writer.headers.set "Content-Type" "text/plain" writer.write_headers 404 writer.write "Not found: $resource" return if not query.parameters.is-empty: Element := query.parameters["Element"] element-size := int.parse Element bucket["element"] = element-size // Save new element size to flash bucket. writer.headers.set "Location" "index.html" writer.write_headers 302 return if result is string: result = result.substitute: look_up_variable it writer.headers.set "Content-Type" (mime_type resource) if compression_type resource: writer.headers.set "Content-Encoding" (compression_type resource) writer.write result
floitsch
floitsch 08/30/2023 10:22 PM
Lgtm
bmentink
bmentink 08/30/2023 10:22 PM
Huh?
floitsch
floitsch 08/30/2023 10:23 PM
Looks good to me.
Standard clause used in Google reviews :🙂:
bmentink
bmentink 08/30/2023 10:23 PM
Ha ... great thanks :🙂:
bmentink
bmentink 08/31/2023 01:56 AM
@floitsch Quick question: Is there a way to push web page refresh action to the client's web page periodically. Is there any Toit support to do this?
bmentink
bmentink 08/31/2023 01:58 AM
Thinking something like this: <meta http-equiv="refresh" content="5; URL=http://www.yourdomain.xx/yourpage.php">
bmentink
bmentink 08/31/2023 02:00 AM
or header( 'refresh: 5; url=http://www.example.net' ); but know idea how to use these .. just found these tricks googling around(edited)
bmentink
bmentink 08/31/2023 03:07 AM
Ha, just doing <meta http-equiv="refresh" content="5 /> in the page head section did the trick ..
👍1
bmentink
bmentink 08/31/2023 03:08 AM
You learn something every day :😎:
21 messages in total