【问题标题】:Loopback REST connector POST环回 REST 连接器 POST
【发布时间】:2015-12-16 21:14:23
【问题描述】:

我尝试创建一个模型,并连接到我的 API 测试服务器。

这是 REST 数据源配置:

"postsREST": {
"name": "postsREST",
"connector": "rest",
"operations": [{
    "template": {
        "method": "GET",
        "url": "http://localhost:3001/posts"
    },
    "functions": {
        "find": []
    }
}, {
    "template": {
        "method": "POST",
        "url": "http://localhost:3001/posts",
        "headers": {
            "accept": "application/json",
            "content-type": "application/json"
        },
        "query": {
            "title": "{^title}",
            "author": "{^author}"
        },
        "body": {
            "title": "{^title}",
            "author": "{^author}"
        }
    },
    "functions": {
        "create": [
            "title",
            "author"
        ]
    }
}]

}

问题是,当我使用资源管理器时,生成的请求 url 是这样的:

http://localhost:3000/api/posts/create?title=f&author=f

代替:

http://localhost:3000/api/posts

我做错了什么?也许有新的文档?

谢谢。

【问题讨论】:

  • 我注意到您在文件中使用端口 3001 (localhost:3001),在浏览器中使用端口 3000,是不是错字?
  • REST 数据源连接器旨在供您的模型用于访问外部 API……这似乎是以循环方式访问同一模型。你到底想完成什么?
  • 当然是外部的。它只是例如。从代码中可以看出,问题在于 post url 的行为类似于 get。 url是本地的(3000端口),3001是外部的。

标签: node.js rest loopbackjs strongloop


【解决方案1】:

如果您不希望参数成为请求 URL 的一部分,则应使用 form 而不是 reqbodyreqbody 会将您的参数附加到请求 URL。

使用form 将发送您的参数,就像使用POST 方法提交表单一样,因此作为请求正文的一部分。

因此,请在代码中的 template 部分尝试以下方式:

"template": {
        "method": "POST",
        "url": "http://localhost:3001/posts",
        "headers": {
            "accept": "application/json",
            "content-type": "application/json"
        },
        "form": {
            "title": "{^title}",
            "author": "{^author}"
        }
    },

此外,我认为在reqbody 属性中添加相同的参数没有任何意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    相关资源
    最近更新 更多