[英]Nodejs express Request body undefined
我無法從 post 請求的請求正文中讀取參數我知道這已經被問過好幾次了,但所有的答案都說在調用 app.use() 之前調用正文解析器我這樣做了,也調用了 bodyparser .urlencode() 並且它仍然給我 undefined 每當我 console.log(req.body) 或只是打印 {} (一個空對象)
這是我的代碼
const bodyParser=require('body-parser');
const express=require('express');
const app=express();
app.use(
bodyParser.urlencoded({
extended: false
})
)
app.use(bodyParser.json())
app.post('/endpoint', (req, res) => {
console.log(req.body);
res.status(400).send({ "ReturnMsg": "User Already Exits" });
})
// the port where the application run
const port = process.env.PORT || 6001;
app.listen(port, () => console.log(`Listening on port ${port}...`));
所以form-data
不通過支持body-parser
,一般multipart/form-data
(mime類型的form-data
)被用於上載文件。 大多數 API 相關案例都使用JSON
或XML
,當您從HTML
發布<form>
,它通常會作為application/x-www-form-urlencoded
。
來自application/x-www-form-urlencoded 或 multipart/form-data 的一行摘錄? :
概括; 如果您要傳輸二進制(非字母數字)數據(或非常大的有效負載),請使用 multipart/form-data。 否則,使用 application/x-www-form-urlencoded。
現在,從郵遞員POST ,
將設置更改為x-www-form-urlencoded
或raw->then select JSON from the dropdown
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.