curl 使用
使用POST 和 PUT 方法带数据发起http请求:
指明请求类型
-X POST
-X PUT
指明http头
-H "Content-Type: application/x-www-form-urlencoded"
-H "Content-Type: application/json"
请求数据
- form urlencoded:
-d "param1=value1¶m2=value2"
or-d @data.txt
- json:
-d '{"key1":"value1", "key2":"value2"}'
or-d @data.json
例子
POST application/x-www-form-urlencoded
application/x-www-form-urlencoded
,这是发送请求的默认数据格式:
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/data
当然,可以在http头部中指明数据格式:
curl -d "param1=value1¶m2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data
从文件中读取请求数据
curl -d "@data.txt" -X POST http://localhost:3000/data
POST application/json
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data
从文件中读取请求数据
curl -d "@data.json" -X POST http://localhost:3000/data
data.json
{
"key1":"value1",
"key2":"value2"
}
data.txt
param1=value1¶m2=value2
server.js
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/data', function (req, res) {
console.log(req.body);
res.end();
});
app.listen(3000);