request by POST
用POST送出請求
使用「axios」套件處理。(這裡使用版本為0.21.1)
const axios = require('axios')
axios
.post(url, postData)
.then(res => {
clog(`statusCode: ${res.status}`)
// console.log(res);
if(res.status == 200){
log("=>" + res.statusText);
}
else{
log(`ERR=>${res.status} ${res.statusText} ${JSON.stringify(res.config)}`);
}
})
.catch(error => {
console.error(error)
})
取得的response(範例中的res)是object物件,但無法直接用「JONS.stringify」轉為string(其中有function之類key用到JSON跳脫字元的部分造成問題...),目前只用「console.log」印出後,抽取需要的節點做顯示。
另外有使用NodeJS原生物件「 https」的方法,但較為複雜,想研究可以於參考位置查看。
參考:https://nodejs.dev/learn/make-an-http-post-request-using-nodejs
Last updated
Was this helpful?