NodeJS/Express: PUT 和正文解析器,正文对象为空
NodeJS/Express: PUT and body parser, body object empty
我为博客写了一个简单的休息api。我有以下代码(主文件):
var port = process.env.PORT || 8080;
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var database = require('./config/database');
var app = express();
mongoose.connect(database.url);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
mongoose.connection.once('open', function () {
console.log('database connection established');
});
// Routes
app.use('/api/blog', require('./routes/blogroutes.js').router);
// Start
app.listen(port, function () {
console.log('listening on port ', port);
});
文件 blogroutes.js
如下所示(摘录):
var express = require('express');
var router = express.Router();
var BlogEntry = require('../models/blogEntry');
router.use(function (req, res, next) {
next();
});
router.route('/entry/:year/:month/:day/:nr')
//.get(...).delete(...)
.put(function (req, res) {
console.log(req.body);
});
router.post('/entry', function (req, res) {
console.log(req.body);
});
module.exports = {router: router};
现在的问题如下:当我调用 PowerShell curl -uri http://localhost:8080/api/blog/entry/2016/3/2/3 -method put -body @{title='my first entry';text='Lorem ipsum'}
时,控制台输出是 {}
。当我调用 PowerShell curl -uri http://localhost:8080/api/blog/entry -method post -body @{title='my first entry';text='Lorem ipsum'}
时,控制台输出符合预期 { text: 'Wonderful', title: 'My second bike trip' }
。您知道我为什么以及如何访问 put 主体吗?
因为它看起来不像您使用的是 real cURL,而是它似乎类似于 Powershell 的 Invoke-WebRequest
cmdlet,the documentation for Invoke-WebRequest
特别提到 application/x-www-form-urlencoded
的 Content-Type
只会针对 POST
请求发送。因此,您需要通过指定额外的命令行选项为非 POST
请求显式设置 Content-Type
:-ContentType "application/x-www-form-urlencoded"
.
我为博客写了一个简单的休息api。我有以下代码(主文件):
var port = process.env.PORT || 8080;
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var database = require('./config/database');
var app = express();
mongoose.connect(database.url);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
mongoose.connection.once('open', function () {
console.log('database connection established');
});
// Routes
app.use('/api/blog', require('./routes/blogroutes.js').router);
// Start
app.listen(port, function () {
console.log('listening on port ', port);
});
文件 blogroutes.js
如下所示(摘录):
var express = require('express');
var router = express.Router();
var BlogEntry = require('../models/blogEntry');
router.use(function (req, res, next) {
next();
});
router.route('/entry/:year/:month/:day/:nr')
//.get(...).delete(...)
.put(function (req, res) {
console.log(req.body);
});
router.post('/entry', function (req, res) {
console.log(req.body);
});
module.exports = {router: router};
现在的问题如下:当我调用 PowerShell curl -uri http://localhost:8080/api/blog/entry/2016/3/2/3 -method put -body @{title='my first entry';text='Lorem ipsum'}
时,控制台输出是 {}
。当我调用 PowerShell curl -uri http://localhost:8080/api/blog/entry -method post -body @{title='my first entry';text='Lorem ipsum'}
时,控制台输出符合预期 { text: 'Wonderful', title: 'My second bike trip' }
。您知道我为什么以及如何访问 put 主体吗?
因为它看起来不像您使用的是 real cURL,而是它似乎类似于 Powershell 的 Invoke-WebRequest
cmdlet,the documentation for Invoke-WebRequest
特别提到 application/x-www-form-urlencoded
的 Content-Type
只会针对 POST
请求发送。因此,您需要通过指定额外的命令行选项为非 POST
请求显式设置 Content-Type
:-ContentType "application/x-www-form-urlencoded"
.