2015-11-13 15 views
5

これは複数回尋ねられたことがわかりましたが、私は周りを見回していて、まだ私の問題の答えを見つけることができません。Express + Postman、req.body is empty

私のコードですが、ルートを定義する前にボディパーサーを使用して設定してください。私はPOST関数をテストしているだけなので、bodyParserで.json()を使用していますが、app.use(bodyParser.urlencoded({extended:true}))でも試みました。

var express = require('express'), 
    bodyParser = require('body-parser'), 
    app = express(); 

app.use(bodyParser.json()); 
app.set('port', (process.env.PORT || 5000)); 

app.listen(app.get('port'), function() { 
    console.log("Node app is running at localhost:" + app.get('port')) 
}); 

app.post('/itemSearch', function(req, res) { 
    //var Keywords = req.body.Keywords; 
    console.log("Yoooooo"); 
    console.log(req.headers); 
    console.log(req.body); 
    res.status(200).send("yay"); 
}); 

ここでは、このルートをテストするためにPostmanを使用します。ここenter image description here

と は、私は私は本当に任意のヘルプをお願い申し上げます。この時点で

Node app is running at localhost:5000 
Yoooooo 
{ host: 'localhost:5000', 
    connection: 'keep-alive', 
    'content-length': '146', 
    'cache-control': 'no-cache', 
    origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop', 
    'content-type': 'multipart/form-data; boundary=----WebKitFormBoundarynJtRFnukjOQDaHgU', 
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', 
    'postman-token': '984b101b-7780-5d6e-5a24-ad2c89b492fc', 
    accept: '*/*', 
    'accept-encoding': 'gzip, deflate', 
    'accept-language': 'en-GB,en-US;q=0.8,en;q=0.6' } 
{} 

を受信応答です。ありがとう。

答えて

7

あなたはボディパーサーを使用する必要があります私の知る限り:次にhttps://github.com/expressjs/body-parser

bodyParser = require('body-parser').json(); 
app.post('/itemSearch', bodyParser, function(req, res) { 
    //var Keywords = req.body.Keywords; 
    console.log("Yoooooo"); 
    console.log(req.headers); 
    console.log(req.body); 
    res.status(200).send("yay"); 
}); 

raw JSONとして身体を設定する郵便配達してみてください:私は変更する必要性を実現して数時間を過ごした後

{ 
    "test": "yay" 
} 
+2

ありがとうございます。問題は、PostManでフォームデータを使ってリクエストを送信していたことです。 – seongju

23

JSONの郵便配達員のRawタイプenter image description here

+0

ありがとうございました! – mimic

+0

誰かがここを見ている場合は、Javascriptヘッダーではなく、PostmanのJSONを使用していることを確認してください。私はJavascriptを持っていたので動作しません。 – k00k

+0

私のために働いた。ありがとう!! –