From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ricardo Wurmus Newsgroups: gmane.lisp.guile.user Subject: Re: Operate upon POST request with Guile webserver Date: Sun, 21 Aug 2022 21:47:16 +0200 Message-ID: <87r1191ia1.fsf@elephly.net> References: <828eadbc-c2bf-c4ab-0833-96c55011d36c@firemail.cc> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15073"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.8.7; emacs 28.1 Cc: guile-user@gnu.org To: Gentoo Arch Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Aug 21 21:58:24 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 1oPr5T-0003jz-C5 for guile-user@m.gmane-mx.org; Sun, 21 Aug 2022 21:58:23 +0200 Original-Received: from localhost ([::1]:45870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oPr5S-0001CZ-Cb for guile-user@m.gmane-mx.org; Sun, 21 Aug 2022 15:58:22 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPr57-0001CQ-5i for guile-user@gnu.org; Sun, 21 Aug 2022 15:58:01 -0400 Original-Received: from sender4-of-o50.zoho.com ([136.143.188.50]:21012) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oPr55-0006id-0E for guile-user@gnu.org; Sun, 21 Aug 2022 15:58:00 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1661111873; cv=none; d=zohomail.com; s=zohoarc; b=azbnmnsXJRFEjEh5FiHkp4mp4f7UD1MA1kTntx2TeIKfSCKM8QHIlVtwvkGNtjsBljF2KmYBERcSVr0QlRRgIyrbtbywJF5ti9H9AGLBY4ky6hUrWiJER4ANOleQONURdqGQS7OguVYABBxUnCidyALOhAdlImOaw9G/oocBbPM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661111873; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=abjaDgwSjAnHyeUod3CRZRSUwBg4N/IxiI1KapJ8LSE=; b=Hz1AC0n2weoBZG/eiDQLcLJv/J8HuqxQ6GiMNo74N8ASWfnZS1hKsZWKF/IGz/VAyZdHH7tsJg9HieGsLdwXMC6L374rwVS1RwlwRH7qZpTK4AcwMbrHIMklDmlEka7wgBIhilt+4/LHGYl8A+kFTYFXNuLvnwl9eL9Jaz0TuII= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=elephly.net; spf=pass smtp.mailfrom=rekado@elephly.net; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1661111873; s=zoho; d=elephly.net; i=rekado@elephly.net; h=References:From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:In-reply-to:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Reply-To; bh=abjaDgwSjAnHyeUod3CRZRSUwBg4N/IxiI1KapJ8LSE=; b=R670mdiks3nx5Qlfgg44cnRgo6lOl/WDRs6os0Bsqhg2D7R3GCwyAaNQDnrMUoIb +2NuO6HUmDA9c8yT3r4mMCe+9KOvMGTzR7a25C2pgHiZJ3v2yy7ffGKpYKivdMJEwjQ 27Xd3cxZ8MTn07mT2hCauNpMVX0XhL+d/FChIl+A= Original-Received: from localhost (i5E862DA0.versanet.de [94.134.45.160]) by mx.zohomail.com with SMTPS id 1661111865426897.12335399309; Sun, 21 Aug 2022 12:57:45 -0700 (PDT) In-reply-to: <828eadbc-c2bf-c4ab-0833-96c55011d36c@firemail.cc> X-URL: https://elephly.net X-PGP-Key: https://elephly.net/rekado.pubkey X-PGP-Fingerprint: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC X-ZohoMailClient: External Received-SPF: pass client-ip=136.143.188.50; envelope-from=rekado@elephly.net; helo=sender4-of-o50.zoho.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham 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:18537 Archived-At: Hi, Gentoo Arch writes: > I'm trying to figure out how Guile webserver works to develop a simple > BBS=C2=A0 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. Daniel already solved your immediate problem, but I wanted to show you a common pattern many of us use to build web servers: --8<---------------cut here---------------start------------->8--- (define* (render-html sxml #:optional (headers '())) (list (append '((content-type . (text/html))) headers) (lambda (port) (sxml->xml sxml port)))) (define (request-path-components request) (split-and-decode-uri-path (uri-path (request-uri request)))) (define (controller request body) (match (cons (request-method request) (request-path-components request)) (('POST "api" "container" id "launch") (render-html `(html (head (title "whatever")) (body "who cares")))) (('PUT "api" "container" id "connect") ...) (('DELETE "api" "container" id) ...) (('GET "api" "containers") ...) (('GET "api" "container" id "info") ...) (_ (not-found (request-uri request))))) (define (handler request body) (format (current-error-port) "~a ~a~%" (request-method request) (uri-path (request-uri request))) (if (getenv "DEBUG") (call-with-error-handling (lambda () (apply values (controller request body)))) (apply values (controller request body)))) (define (run-my-server port) (run-server handler #:port port)) --8<---------------cut here---------------end--------------->8--- The controller uses match on the method and the path, which makes for pretty clear code. You can bind parts of the path to variables and operate on them. The =E2=80=9Cvalues=E2=80=9D stuff is handled in =E2=80=9Chandler=E2=80=9D = here, but it also be done directly in the controller. To operate on the POST payload, you=E2=80=99d need to parse the form data f= rom the body, which is available to the controller. See also guile-webutils for some handy tools. Hope this helps! --=20 Ricardo