自定义Drupal 8 REST资源上不允许使用POST方法
我创建了一个自定义的自定义Drupal 8 REST资源上不允许使用POST方法,drupal,Drupal,我创建了一个自定义的RestResource,它看起来像这样: /** * Provides a resource to get/create content data. * * @RestResource( * id = "arcelor_content", * label = @Translation("Arcelor content"), * uri_paths = { * "canonical" = "/api/content/{type}" *
RestResource
,它看起来像这样:
/**
* Provides a resource to get/create content data.
*
* @RestResource(
* id = "arcelor_content",
* label = @Translation("Arcelor content"),
* uri_paths = {
* "canonical" = "/api/content/{type}"
* }
* )
*/
class ContentResource extends ResourceBase {
public function get($type) {
// Works
}
public function post($type) {
// Doesn't work
}
}
我已经在RestUI中启用了资源并设置了权限
GET方法工作正常,但当我尝试发布时,会出现以下错误:
{
"message": "No route found for \"POST /api/content/buffer\": Method Not Allowed (Allow: GET)"
}
方法不允许!即使权限已设置,post已启用,缓存已刷新一百万次
我发现可以通过向phpdoc标记添加另一个uri_路径来修复它,所以我做了:
/**
* Provides a resource to get/create content data.
*
* @RestResource(
* id = "arcelor_content",
* label = @Translation("Arcelor content"),
* uri_paths = {
* "canonical" = "/api/content/{type}",
* "https://www.drupal.org/link-relations/create" = "/api/content/{type}"
* }
* )
*/
不幸的是,这没有起到任何作用,我仍然得到“不允许”的错误
那么,有人知道这里发生了什么吗?2件事导致了“不允许”问题:
Content-Type
标题设置为application/hal+json
,这是它们唯一可以接受的。即使您计划对常规JSON数据做一些不同的事情,您也必须以某种方式解决这个问题(我没有做到)X-CSRF-Token
头,您可以通过转到/rest/session/Token
现在我不再得到“不允许”的错误!不幸的是,因为主体需要是
hal+json
,我现在得到了一个“发生了致命错误:类不存在”
错误。对于我来说,在Drupal 8.2.6下,解决方法不允许用于POST的问题是我忘记在资源定义注释中指定create URI:
/**
* Provides a resource for clients subscription to updates.
*
* @RestResource(
* id = "updates_subscription",
* label = @Translation("Updates subscription"),
* uri_paths = {
* "canonical" = "/api/updates-subscription",
* "https://www.drupal.org/link-relations/create" = "/api/updates-subscription"
* }
* )
*/
事实上,如果您忘记指定“https://www.drupal.org/link-relations/create“
URI路径,Drupal默认为资源id路径,在本例中,updates\u subscription
您可以在创建REST资源插件一节中阅读这篇文章
不需要
应用程序/hal+json
内容类型也不需要使用X-CSRF-Token
您可以解决这个问题吗?