【问题标题】:Unable to access query string parameters in a post request using express无法使用 express 访问 post 请求中的查询字符串参数
【发布时间】:2016-09-09 04:49:22
【问题描述】:

我正在尝试通过 POST 请求获取查询参数,但它似乎没有收到它们。

app.use(bodyParser.urlencoded({extended: false }));

app.post('/pubs/:id/submit', function (req, res, next) {

    console.log("query: " + JSON.stringify(req.query)) //prints {}

我尝试通过 Postman 和 curl 发送请求,请求格式正确。

【问题讨论】:

标签: node.js rest express body-parser


【解决方案1】:

使用body-parser解析body。

var bodyParser=require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

Example#1 by parameters

POST http://localhost:3000/hello/id/123/access_token/1234556ccc/name/hola

console.log(JSON.stringify(req.params))

查询示例#2

POST http://localhost:3000/hello?text=abc&access_token=1234556ccc&name=hola

`console.log(JSON.stringify(req.query))`

【讨论】:

  • 只打印出查询:{"id":"21421123"}
  • 正如我在您的帖子中看到的,您刚刚通过id
  • 我使用 ?accessToken=fet 提交完整的 url
  • 我刚刚更新了两种方式的答案。请看一下
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多