gpt4 book ai didi

request - 使用 Rascal 获取用户输入

转载 作者:行者123 更新时间:2023-12-04 08:26:38 25 4
gpt4 key购买 nike

Rascal 目前正在托管我的简单网络服务器。在这里,我为使用 HTML textarea 标签的用户提供了一个输入,以及一个提交按钮。但是,我不知道如何在用户提交输入数据时向他们请求输入数据。我也没有看到太多关于它的文档,所以任何帮助将不胜感激!

最佳答案

假设您使用 Content和/或 util::Webserver从用于从 Rascal 提供内容的库中,您始终提供类型为 Response (Request) 的函数。到服务器。这个函数完成了从服务 index.html 开始的所有工作接收表单输入和处理 XMLHttpRequests。您所要做的就是编写函数的替代方案。
您可以获得的请求类型在 Content.rsc 中定义如下:

data Request (map[str, str] headers = (), map[str, str] parameters = (), map[str,str] uploads = ())
= get (str path)
| put (str path, Body content)
| post(str path, Body content)
| delete(str path)
| head(str path)
;
响应定义为:
data Response 
= response(Status status, str mimeType, map[str,str] header, str content)
| fileResponse(loc file, str mimeType, map[str,str] header)
| jsonResponse(Status status, map[str,str] header, value val, bool implicitConstructors = true, bool implicitNodes = true, str dateTimeFormat = "yyyy-MM-dd\'T\'HH:mm:ss\'Z\'")
;
在下面的示例中,我使用了便利的实用程序函数,例如 Content::response(str)它用正确的 HTTP 状态和 mimetypes 包装一个 html 字符串。
例子:
// this serves the initial form
Response myServer(get("/"))
= response("\<p\>What is your name?\</p\>
'\<form action=\"/submit\" method=\"GET\"\>
' \<input type=\"text\" name=\"name\" value=\"\"\>
'\</form\>
");

// // this responds to the form submission, now using a function body with a return (just for fun):
Response myServer(p:get("/submit")) {
return response("Hello <p.parameters["name"]>!");
}

// in case of failing to handle a request, we dump the request back for debugging purposes:
default Response myServer(Request q) = response("<q>");
现在我们可以直接从 REPL 提供服务。内容将显示在 Eclipse 编辑器窗口或您的默认浏览器中,并在 Rascal 的内部应用程序服务器中最后一次交互后保持可用 30 分钟:
rascal>content("test", myServer)
Serving 'test' at |http://localhost:9050/|
或者我们可以自己提供服务,然后浏览到 http://localhost:10001来测试服务器。完成后我们必须手动关闭它:
rascal>import util::Webserver;
ok
rascal>serve(|http://localhost:10001|, myServer)
ok
rascal>shutdown(|http://localhost:10001|)
The initially served page in an editor window
The response after for submission

关于request - 使用 Rascal 获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65228060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com