guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Operate upon POST request with Guile webserver


From: Gentoo Arch
Subject: Re: Operate upon POST request with Guile webserver
Date: Sat, 20 Aug 2022 15:03:19 +0300
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0

Hi Daniel,

Thanks a lot, that worked! I also can print debugging output to server log and do other useful stuff.

I totally forgot about (begin); procedural habit I guess.

On 8/19/22 14:00, Daniel Meißner wrote:
Hi,

Am 17. August 2022 13:50:46 MESZ schrieb Gentoo Arch <gentoocore@firemail.cc>:
Hi,

I'd like to ask for your advice.

I'm trying to figure out how Guile webserver works to develop a simple BBS  and 
I'm kinda stuck with POST request.

My Guile script generates a form on any path and the form sends to "/post" 
path, where I can easily render content sent by the form.

(define (show-page request body)
   (if (equal? (get-path request) ; get-path is my other function to retrieve 
the path
               '("post"))
       (values '((content-type . (text/plain)))
                body)
       (respond ; literally a respond template from webserver doc
        `((h1 "Oops!")
      (p (@ (id "test")) "The path: " ,(get-path request))
      (form (@ (method "POST") (action "/post"))
            (label (@ (for "test")) "Content: ")
            (input (@ (id "test") (type "text") (name "content")))
            (input (@ (type "submit") (value "Submit"))))))))

But I would like to operate upon the content the form sends like web apps 
usually do: insert it in the database or simply write to file. So my question 
is how I can proceed with something like that:

(define tf (open-file "test-file.txt" "a"))

(define (show-page request body)
   (if (equal? (get-path request)
               '("post"))
       ((values '((content-type . (text/plain)))
               body)
        (display body tf)); I'd like to write/append body to the file, but it 
does nothing
       (respond
        `((h1 "Oops!")
      (p (@ (id "test")) "The path: " ,(get-path request))
      (form (@ (method "POST") (action "/post"))
            (label (@ (for "test")) "Content: ")
            (input (@ (id "test") (type "text") (name "content")))
            (input (@ (type "submit") (value "Submit"))))))))

If I remove (values), it does write to a file (with a complaint in webserver output), but 
I'd also like to add some kind of redirect from "/post" page after the script 
has written stuff to file and I can't use two functions per response.

Could you please advise how I can achieve this functionality?
I think you probably want to replace the true-branch of your `if' form with

(begin
   (display body tf)
   (values '((content-type . (text/plain)) body))

It is important that the handler returns two values, so the `values' form must 
come last in the sequence. These two return values make up the response.

Best
Daniel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]