From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Gentoo Arch Newsgroups: gmane.lisp.guile.user Subject: Re: Operate upon POST request with Guile webserver Date: Sat, 20 Aug 2022 15:03:19 +0300 Message-ID: References: <828eadbc-c2bf-c4ab-0833-96c55011d36c@firemail.cc> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2997"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Cc: guile-user@gnu.org To: =?UTF-8?Q?Daniel_Mei=c3=9fner?= Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Aug 20 14:03:56 2022 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oPNCm-0000a4-GG for guile-user@m.gmane-mx.org; Sat, 20 Aug 2022 14:03:56 +0200 Original-Received: from localhost ([::1]:56438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oPNCk-0004nJ-Us for guile-user@m.gmane-mx.org; Sat, 20 Aug 2022 08:03:54 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44382) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPNCV-0004mk-Tj for guile-user@gnu.org; Sat, 20 Aug 2022 08:03:39 -0400 Original-Received: from [37.120.193.124] (port=33070 helo=mail.cock.li) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPNCN-0000vh-UZ for guile-user@gnu.org; Sat, 20 Aug 2022 08:03:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=firemail.cc; s=mail; t=1660997000; bh=2Ydkg3+SD3OM/qi4Buo1fhwT13YnKvhwg0XK0apWJYg=; h=Date:Subject:To:References:Cc:From:In-Reply-To:From; b=m+4NFqaZ+wuhQbcjEqBPHvhnchEQxNR5CiTwoFMIC3eSNDhfQ0gk3M3HW2z6EstF4 Hk2ke+OWYv2f0FvDJDwKPW5qC6svgtpsx58/KAGigPYpvjoaHCh0eNvXxHO+xX53gK qg/hCQJKUKDQHiiY2tMuia5/c9F26azLjzNi9IcURe9Ec1BoyQk2HQ/hLR49EPzS0v 9W+l4xu40gmGUn4UZRGLXP+YzrD96t8njK+3IGd8xHVbKNcL4U3glZDD+q/cbIFBk9 ntOpBEhLrCUgkTiKD5ptcB9/CBeiMZiFNV7g38flDJQkOBVklpbfgR5Sx0qLTBOKBa fDXK/ujERKVnA== Content-Language: en-US In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 37.120.193.124 (failed) Received-SPF: pass client-ip=37.120.193.124; envelope-from=gentoocore@firemail.cc; helo=mail.cock.li X-Spam_score_int: -12 X-Spam_score: -1.3 X-Spam_bar: - X-Spam_report: (-1.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.001, RDNS_NONE=0.793, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:18532 Archived-At: 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 : >> 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 >