2017-01-08 40 views
4

我遇到類似於this question和其他幾個症狀的問題,雖然答案對我沒有幫助。我試圖通過一個簡單的HTML表單將密碼提交給一個Node應用程序,但請求主體保持空白。通過HTML表單發出POST請求時,請求正文爲空

服務器:

app.use(bodyParser.urlencoded({extended: true})); 

router.post('/login', (req, res) => { 
    console.log(req.body); 
    console.log(req.headers['content-type']); 
}); 

形式:

<form action="/login" method="post"> 
    <input type="password" id="password"> 
    <button type="submit">Log In</button> 
</form> 

如果我提交表單,我得到如下:

{} // req.body 
'application/x-www-form-urlencoded' // req.headers['content-type'] 

但是,如果我嘗試捲曲的端點,我收到一個非空的req.body

$ curl -X POST localhost:5000/login -d 'password=testpw' 

// Output 
{ password: 'testpw' } 
'application/x-www-form-urlencoded' 

我在做什麼錯?

+0

什麼明確的版本? – Alex

+0

快遞版本4 – ericgio

回答

6

問題是在你的形式

<form action="/login" method="post"> 
    <input type="password" id="password"> 
    <button type="submit">Log In</button> 
</form> 

input沒有一個name元素

應該

<input name="password" type="password" id="password"> 
+0

Ughhhhhhhhh。謝謝。 – ericgio

+0

沒問題,容易犯錯! :) – Alex