I know this has been asked multiple times, but I have been looking around and still can't find an answer to my problem.
我知道这已被多次询问,但我一直在四处寻找,仍然找不到我的问题的答案。
Here is my code, I make sure to use and configure body parser before defining the routes. I'm only using .json() with bodyParser because right now I'm only testing a POST function, but I've even tried with app.use(bodyParser.urlencoded({ extended: true }));
这是我的代码,我确保在定义路由之前使用和配置主体解析器。我只使用.json()和bodyParser,因为我现在只测试一个POST函数,但我甚至尝试过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");
});
Here is how I use Postman to test this route.
以下是我如何使用Postman来测试这条路线。
and here is the response I receive
这是我收到的回复
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' }
{}
At this point I would really appreciate any help. Thanks.
在这一点上,我真的很感激任何帮助。谢谢。
3 个解决方案
#1
9
AFAIK you need to use the Body-Parser : https://github.com/expressjs/body-parser
AFAIK你需要使用Body-Parser: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");
});
Then try with PostMan setting the body as raw
json:
然后尝试使用PostMan将身体设置为原始json:
{
"test": "yay"
}
#2
#3
0
In my case, I solve it By Adding "type":"text"
to urlencoded
item int the exported collection json
file generated by postman. I observe it because there are some of my request is successfully done. The difference is the missing type
field in the json generated postman collection file. This problem also happen to my teammate.
在我的情况下,我解决它通过将“type”:“text”添加到urlencoded item int postman生成的导出集合json文件中。我观察它,因为我的一些要求已成功完成。区别在于json生成的postman集合文件中缺少的类型字段。这个问题也发生在我的队友身上。
before (failed request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}" }, { "key": "password", "value": "{{userPassword}}" } ] }
之前(失败请求):“正文”:{“模式”:“urlencoded”,“urlencoded”:[{“key”:“email”,“value”:“{{userEmail}}”},{“key” :“密码”,“值”:“{{userPassword}}”}]}
after (successful request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}", "type": "text" }, { "key": "password", "value": "{{userPassword}}", "type": "text" } ] }
之后(成功请求):“body”:{“mode”:“urlencoded”,“urlencoded”:[{“key”:“email”,“value”:“{{userEmail}}”,“type”:“ text“},{”key“:”password“,”value“:”{{userPassword}}“,”type“:”text“}]}
I also write parser script in javascript language to handle it.
我还用javascript语言编写解析器脚本来处理它。
const fs = require('fs');
let object = require(process.argv[2]);
function parse(obj) {
if(typeof obj === 'string') return;
for(let key in obj) {
if(obj.hasOwnProperty(key)) {
if(key === 'urlencoded') {
let body = obj[key];
for(let i = 0;i < body.length;i++) {
body[i].type = "text";
}
}
else parse(obj[key]);
}
}
}
parse(object);
fs.writeFile('ParsedCollection.json', JSON.stringify(object, null, '\t'), function(err){
//console.log(err);
});
Just run it in terminal node parser.js <json postman collection file path>
and it will output in ParsedCollection.json
file. After that import this file into postman.
只需在终端节点parser.js
#1
9
AFAIK you need to use the Body-Parser : https://github.com/expressjs/body-parser
AFAIK你需要使用Body-Parser: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");
});
Then try with PostMan setting the body as raw
json:
然后尝试使用PostMan将身体设置为原始json:
{
"test": "yay"
}
#2
28
After spending a few hours I realized need to change the Raw type in postman to JSON
花了几个小时后,我意识到需要将postman中的Raw类型更改为JSON
#3
0
In my case, I solve it By Adding "type":"text"
to urlencoded
item int the exported collection json
file generated by postman. I observe it because there are some of my request is successfully done. The difference is the missing type
field in the json generated postman collection file. This problem also happen to my teammate.
在我的情况下,我解决它通过将“type”:“text”添加到urlencoded item int postman生成的导出集合json文件中。我观察它,因为我的一些要求已成功完成。区别在于json生成的postman集合文件中缺少的类型字段。这个问题也发生在我的队友身上。
before (failed request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}" }, { "key": "password", "value": "{{userPassword}}" } ] }
之前(失败请求):“正文”:{“模式”:“urlencoded”,“urlencoded”:[{“key”:“email”,“value”:“{{userEmail}}”},{“key” :“密码”,“值”:“{{userPassword}}”}]}
after (successful request): "body": { "mode": "urlencoded", "urlencoded": [ { "key": "email", "value": "{{userEmail}}", "type": "text" }, { "key": "password", "value": "{{userPassword}}", "type": "text" } ] }
之后(成功请求):“body”:{“mode”:“urlencoded”,“urlencoded”:[{“key”:“email”,“value”:“{{userEmail}}”,“type”:“ text“},{”key“:”password“,”value“:”{{userPassword}}“,”type“:”text“}]}
I also write parser script in javascript language to handle it.
我还用javascript语言编写解析器脚本来处理它。
const fs = require('fs');
let object = require(process.argv[2]);
function parse(obj) {
if(typeof obj === 'string') return;
for(let key in obj) {
if(obj.hasOwnProperty(key)) {
if(key === 'urlencoded') {
let body = obj[key];
for(let i = 0;i < body.length;i++) {
body[i].type = "text";
}
}
else parse(obj[key]);
}
}
}
parse(object);
fs.writeFile('ParsedCollection.json', JSON.stringify(object, null, '\t'), function(err){
//console.log(err);
});
Just run it in terminal node parser.js <json postman collection file path>
and it will output in ParsedCollection.json
file. After that import this file into postman.
只需在终端节点parser.js