[英]Redirect the user to the URL of REST API after sending the post request using Needle
我正在使用 REST API 作为对地址https://example.com/api
的 POST 请求的结果返回 HTML 网页(而不是 JSON 或其他数据格式)。
通常在前端处理这种情况并不难使用这样的代码来管理:
<form action="https://example.com/api" method="POST"> <input type="text" name="param1" value="test1"> <input type="text" name="param2" value="test2"> <input type="submit"> </form>
在这种情况下,仅按下提交按钮会导致使用正确的参数加载 URL https://example.com/api
。 但是,我无法使用 Node.js 和 Needle 库在服务器端代码上复制它。
我使用了这样的代码片段:
let params = {
param1: "test1",
param2: "test2"
};
let options = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
needle.post("https://example.com/api", params, options, function (err, response) {
console.log(response.body);
//what should i do here?
});
但在这一点上,我不知道如何将客户端重定向到该地址。 response.body
属性包含网页 HTML 但直接使用以下方式发送:
res.send(response.body);
由于缺少 css 个文件和相关 URL,导致网页外观失真。 同样直接重定向到回调中的网页似乎不起作用:
res.redirect("https://example.com/api");
我想我自己想出来了,我们可以像 GET 请求一样使用 URL 查询:
res.redirect("https://example.com/api?param1=test1¶m2=test2");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.