gpt4 book ai didi

rest - Drupal 8 REST 资源 POST 方法

转载 作者:行者123 更新时间:2023-12-04 23:01:17 25 4
gpt4 key购买 nike

我需要帮助才能在 Drupal 8 中使用 POST 方法实现自定义 REST API。

我有一个 REST 资源插件类和一个 get() 方法。
我能够通过 GET 方法访问此资源并且工作正常。

我在同一个类中有一个 post() 方法,但无法通过 POST 方法访问资源,即使通过“REST UI”模块查看时显示“POST”也是如此。

REST UI Settings page

但是当通过 POST 方法访问时,它显示以下错误。

{"message":"No route found for \u0022POST \/iot\/location\/\u0022: Method Not Allowed (Allow: GET)"}

我正在使用 POSTMAN Chrome 扩展程序,截图如下
Testing REST API

我使用以下博客编写了 REST API,我的代码与这篇博文中显示的代码几乎相同

http://enzolutions.com/articles/2014/12/16/how-to-create-a-rest-resource-in-drupal-8/

提前致谢。

最佳答案

确保您已设置 uri_paths在您的 REST 资源中,包括“https://www.drupal.org/link-relations/create”。

例如,您可能有这样的事情:

/**
* My REST Resource
*
* @RestResource(
* id = "my_rest_resource",
* label = @Translation("My REST Resource"),
* uri_paths = {
* "canonical" = "/my_rest/resource"
* }
* )
*/

要发布到上述资源,您将使用端点 /my_rest_resource .如果您想发布到 /my_rest/resource而是添加链接关系条目:
/**
* My REST Resource
*
* @RestResource(
* id = "my_rest_resource",
* label = @Translation("My REST Resource"),
* uri_paths = {
* "canonical" = "/my_rest/resource",
* "https://www.drupal.org/link-relations/create" = "/my_rest/resource"
* }
* )
*/

添加 https://www.drupal.org/link-relations/create 后可能需要清除缓存.我发现 GET 可能在第一个示例中按预期工作,而 POST 失败并出现错误“405 方法不允许”。

Drupal 的文档( https://www.drupal.org/docs/8/api/restful-web-services-api/restful-web-services-api-overview)中关于这个主题的措辞可能更清楚一些。

关于rest - Drupal 8 REST 资源 POST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34544890/

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