我运行了以下代码,并在命令提示符下使用了以下语句:
curl -H "Content-Type: application/json" -X POST -d '{"first_name": "Moonis", "last_name": "Rasheed"}' "http://localhost:3000/profile"
和
curl.exe -H "Content-Type: application/json" -X PUT -d '{"first_name": "Osama", "last_name": "Naveed"}' "http://localhost:3000/profile"
这就是我在这张照片中遇到错误的时候。我不知道为什么会发生这种情况,因为根据我的逻辑,它应该在配置文件中添加数据
const express = require('express');
const app = express();
const bodyParser=require('body-parser');
app.use(bodyParser.json());
let profile = {
username: 'azat',
email: '[reducted]',
url: 'http://azat.co'
};
app.get('/profile', (req, res)=>{
res.send(profile);
})
app.post('/profile', (req, res) => {
profile = req.body;
console.log("created",profile);
res.sendStatus(201);
})
app.put('/profile', (req, res)=>{
Object.assign(profile, req.body);
console.log("updated",profile);
res.sendStatus(204);
})
app.delete('/profile', (req, res)=>{
profile ={};
console.log("deleted");
res.sendStatus(204);
})
app.listen(3000);
发布于 2019-02-28 12:05:07
尝试这样设置curl
命令的格式:
curl -H "Content-Type: application/json" -X POST -d "{\"first_name\": \"Moonis\", \"last_name\": \"Rasheed\"}" "http://localhost:3000/profile"
基本上,在data参数两边使用双引号,转义作为json
一部分的内部双引号。
https://stackoverflow.com/questions/54925172
复制相似问题