vibed:如何处理 POST 请求?
vibed: How can I handle POST request?
谁能帮我处理 POST 请求,我读了 docs,但我不清楚如何处理 POST 请求,我从页面发送到振动服务器。
我写了下一个代码:
import vibe.d;
import std.stdio;
void main()
{
auto router = new URLRouter;
router.any("*", &accControl);
router.any("/my", &action);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::", "127.0.0.1"];
listenHTTP(settings, router);
runEventLoop();
}
void accControl(HTTPServerRequest req, HTTPServerResponse res)
{
res.headers["Access-Control-Allow-Origin"] = "*";
}
void action(HTTPServerRequest req, HTTPServerResponse res)
{
// how get string from POST request here. And how get JSON object, if server send it.
}
但是我应该使用什么方法来请求?据我了解,expect POST body 正在发送大量其他数据。
POST 请求正在发送 JQuery:
$.post("http://127.0.0.1:8080", "\"answers_result\":777");
所以我需要得到这个 JSON 并将它与 vibed 一起发送到数据库。但是我无法理解如何处理的问题。
主要内容:
auto router = new URLRouter;
router.post("/url_to_match", &action);
listenHTTP(settings, router);
操作:
void action(HTTPServerRequest req, HTTPServerResponse res)
{
auto answers_result = req.json["answers_result"].to!int;
// ...
}
或者您可以使用 registerRestInterface
.
这是一个示例代码,展示了如何从 vibe.d 中读取 POST 参数:
主要功能:
shared static this()
{
auto router = new URLRouter;
router.post("/url_to_match", &action);
auto settings = new HTTPServerSettings;
settings.port = 3000;
listenHTTP(settings, router);
}
操作:
void action(HTTPServerRequest req, HTTPServerResponse res)
{
// Read first POST parameter named "first_name"
auto firstName = req.form["first_name"];
// Read second POST parameter named "last_name"
auto lastName = req.form["last_name"];
// Prepare output to be sent to client.
auto name = "Hello %s, %s".format(lastName, firstName);
// Send data back to client
res.writeBody(name);
}
构建程序并 运行 它,要在您的本地计算机上试用它,您可以执行以下简单的 curl 请求:
curl --data "first_name=kareem&last_name=smith" "http://localhost:3000/url_to_match"
HTH
谁能帮我处理 POST 请求,我读了 docs,但我不清楚如何处理 POST 请求,我从页面发送到振动服务器。
我写了下一个代码:
import vibe.d;
import std.stdio;
void main()
{
auto router = new URLRouter;
router.any("*", &accControl);
router.any("/my", &action);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::", "127.0.0.1"];
listenHTTP(settings, router);
runEventLoop();
}
void accControl(HTTPServerRequest req, HTTPServerResponse res)
{
res.headers["Access-Control-Allow-Origin"] = "*";
}
void action(HTTPServerRequest req, HTTPServerResponse res)
{
// how get string from POST request here. And how get JSON object, if server send it.
}
但是我应该使用什么方法来请求?据我了解,expect POST body 正在发送大量其他数据。
POST 请求正在发送 JQuery:
$.post("http://127.0.0.1:8080", "\"answers_result\":777");
所以我需要得到这个 JSON 并将它与 vibed 一起发送到数据库。但是我无法理解如何处理的问题。
主要内容:
auto router = new URLRouter;
router.post("/url_to_match", &action);
listenHTTP(settings, router);
操作:
void action(HTTPServerRequest req, HTTPServerResponse res)
{
auto answers_result = req.json["answers_result"].to!int;
// ...
}
或者您可以使用 registerRestInterface
.
这是一个示例代码,展示了如何从 vibe.d 中读取 POST 参数:
主要功能:
shared static this()
{
auto router = new URLRouter;
router.post("/url_to_match", &action);
auto settings = new HTTPServerSettings;
settings.port = 3000;
listenHTTP(settings, router);
}
操作:
void action(HTTPServerRequest req, HTTPServerResponse res)
{
// Read first POST parameter named "first_name"
auto firstName = req.form["first_name"];
// Read second POST parameter named "last_name"
auto lastName = req.form["last_name"];
// Prepare output to be sent to client.
auto name = "Hello %s, %s".format(lastName, firstName);
// Send data back to client
res.writeBody(name);
}
构建程序并 运行 它,要在您的本地计算机上试用它,您可以执行以下简单的 curl 请求:
curl --data "first_name=kareem&last_name=smith" "http://localhost:3000/url_to_match"
HTH