我应该如何格式化我的 POST 数据以测试快速 API 端点?
How should I format my POST data for testing an express API endpoint?
我正在关注:https://www.digitalocean.com/community/tutorials/getting-started-with-the-mern-stack。
我想测试使用 express 构建的 API 端点。我想测试 POST.
节点服务器是 运行,我正在使用 postman 来检查端点是否正常工作。
我不清楚如何格式化 post 数据,我的 POST 请求在发送时出现错误。
我的API如下:
const express = require ('express');
const router = express.Router();
const Todo = require('../models/todo');
router.get('/todos', (req, res, next) => {
//this will return all the data, exposing only the id and action field to the client
Todo.find({}, 'action')
.then(data => res.json(data))
.catch(next)
});
router.post('/todos', (req, res, next) => {
if(req.body.action){
Todo.create(req.body)
.then(data => res.json(data))
.catch(next)
}else {
res.json({
error: "The input field is empty"
})
}
});
router.delete('/todos/:id', (req, res, next) => {
Todo.findOneAndDelete({"_id": req.params.id})
.then(data => res.json(data))
.catch(next)
})
module.exports = router;
我的架构如下:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//create schema for todo
const TodoSchema = new Schema({
action: {
type: String,
required: [true, 'The todo text field is required']
}
})
//create model for todo
const Todo = mongoose.model('todo', TodoSchema);
module.exports = Todo;
在 Postman 中,我的 URL 是“http://localhost:5000/api/todos”,我添加了一个 body,键为“action”,值为作为“asdf”。发送时我得到以下结果:
{
"error": "The input field is empty"
}
能否请您告诉我如何格式化我的 body 数据,以便我可以正确测试我的 POST 端点?
打开 Postman,select 以 POST 请求并点击正文。
Under Body, select raw 并像这样在下面的 space 中插入您的数据,然后从文本更改为 JSON 选项:-
{
“动作”:“asdf”
}
请确保在任何路由处理程序之前将其添加到您的 app.js 文件中
const app = express();
app.use(express.json());
我正在关注:https://www.digitalocean.com/community/tutorials/getting-started-with-the-mern-stack。
我想测试使用 express 构建的 API 端点。我想测试 POST.
节点服务器是 运行,我正在使用 postman 来检查端点是否正常工作。
我不清楚如何格式化 post 数据,我的 POST 请求在发送时出现错误。
我的API如下:
const express = require ('express');
const router = express.Router();
const Todo = require('../models/todo');
router.get('/todos', (req, res, next) => {
//this will return all the data, exposing only the id and action field to the client
Todo.find({}, 'action')
.then(data => res.json(data))
.catch(next)
});
router.post('/todos', (req, res, next) => {
if(req.body.action){
Todo.create(req.body)
.then(data => res.json(data))
.catch(next)
}else {
res.json({
error: "The input field is empty"
})
}
});
router.delete('/todos/:id', (req, res, next) => {
Todo.findOneAndDelete({"_id": req.params.id})
.then(data => res.json(data))
.catch(next)
})
module.exports = router;
我的架构如下:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//create schema for todo
const TodoSchema = new Schema({
action: {
type: String,
required: [true, 'The todo text field is required']
}
})
//create model for todo
const Todo = mongoose.model('todo', TodoSchema);
module.exports = Todo;
在 Postman 中,我的 URL 是“http://localhost:5000/api/todos”,我添加了一个 body,键为“action”,值为作为“asdf”。发送时我得到以下结果:
{
"error": "The input field is empty"
}
能否请您告诉我如何格式化我的 body 数据,以便我可以正确测试我的 POST 端点?
打开 Postman,select 以 POST 请求并点击正文。
Under Body, select raw 并像这样在下面的 space 中插入您的数据,然后从文本更改为 JSON 选项:-
{ “动作”:“asdf” }
请确保在任何路由处理程序之前将其添加到您的 app.js 文件中
const app = express(); app.use(express.json());