【问题标题】:Cant get into GET request when sending data in the URL在 URL 中发送数据时无法进入 GET 请求
【发布时间】:2021-06-30 14:43:53
【问题描述】:

当我尝试在 URL 中发送数据时,我正在使用邮递员测试我的 API,邮递员以 404 Not found 响应。我猜它的格式,但四处搜索我认为它是正确的。谁能给我点灯?

但是当我不在 URL 中发送任何数据时,它工作得很好:

当我尝试在 get 请求中发送数据时:

app.get("/mediciones/sigfox_libelium/:{device}", (req, res) => {
    const { device } = req.params;
    res.json("im in");
    console.log("Im in");
    console.log(device);
    console.log(req.params.device);

当我没有发送任何关于请求的数据时工作正常(它崩溃但它还没有完成):

app.get("/mediciones/sigfox_libelium", (req, res) => {
    const { device} = req.params;
    res.json("im in");
    console.log("Im in");
    console.log(device);
    console.log(req.params.device);

【问题讨论】:

    标签: javascript get postman


    【解决方案1】:

    我猜你是在使用 Express.js 构建 API 对吗?
    我认为您以错误的方式使用req.params

    如果你想要参数名称device
    您必须在 url 路径中使用 :device 而不是 :{device}。 像这样

    app.get("/mediciones/sigfox_libelium/:device", (req, res) => {
        cosnt { device } = req.params // You can get device here
    })
    

    【讨论】:

    • 好的,谢谢,我猜我看错博客了,网址格式不对。
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2015-10-28
    • 2017-10-21
    • 2021-05-23
    相关资源
    最近更新 更多