[英]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.