我为博客写了一个简单的休息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体?
答案 0 :(得分:1)
因为它看起来不像你正在使用真正的 cURL,而是看起来像是Powershell的Invoke-WebRequest
cmdlet,the documentation用于{{1} }具体提到只会Invoke-WebRequest
Content-Type
application/x-www-form-urlencoded
POST
}。因此,您需要通过指定其他命令行选项Content-Type
来为非POST
请求显式设置-ContentType "application/x-www-form-urlencoded"
。