gpt4 book ai didi

javascript - req.body 没有出现在 postman 中

转载 作者:行者123 更新时间:2023-12-03 01:08:42 25 4
gpt4 key购买 nike

I was sending the post request in raw and json format...

this is the header configuration

So heres the code:

//app.js

const port = process.env.PORT || 3000;
const express = require ('express');
const app = express();
const jsonParser = require('body-parser').json;
const routes = require('./routes/routes.js');


app.use(jsonParser());

app.use("/questions", routes);

//All routine stuff I'd say...

app.listen(port,(err)=>{
if (!err)
{console.log(`The express server is running at http://localhost:${port}`)}
});
//routes.js
const express = require('express');
const router = express.Router();

//GET /questions
//return all the questions from the db
router.get('/', function(req, res){
res.json({response: "You sent me a get request"});
});

//POST /questions
//Route for creating questions
router.post("/", function(req, res){
res.json({
response: "you sent me a POST request",
body: req.body

});
});




module.exports = router;

this is the error i get after the post request

这是我不断收到的错误..我不知道应该如何解决它..我只是无法让 req.body 出现在 Postman 中...我不确定我是否错过了任何内容或没有添加任何内容或者也许在齿轮上扔了一把 Spanner ...任何帮助都是值得赞赏的。

//The error is as following
SyntaxError: Unexpected token c in JSON at position 3
at JSON.parse (<anonymous>)
at parse (C:\Restful\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Restful\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Restful\node_modules\raw-body\index.js:224:16)
at done (C:\Restful\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Restful\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)

最佳答案

添加屏幕截图中指定的请求正文,这将起作用,因为对象键需要是字符串:Postman Request body

关于javascript - req.body 没有出现在 postman 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52283120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com