我创建了一个自定义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
}
}
Run Code Online (Sandbox Code Playgroud)
我已启用资源并在RestUI中设置权限。
GET方法工作正常,但是当我尝试发布时出现此错误:
{
"message": "No route found for \"POST /api/content/buffer\": Method Not Allowed (Allow: GET)"
}
Run Code Online (Sandbox Code Playgroud)
方法不允许!即使已设置权限,已启用发布,缓存也已刷新了一百万次,...
我在drupal网站上遇到一个问题,说可以通过在phpdoc标签中添加另一个uri_path来解决此问题,所以我做到了:
/**
* 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}"
* }
* )
*/
Run Code Online (Sandbox Code Playgroud)
不幸的是,它什么也没做,但是我仍然收到“不允许”错误。
那么,有人知道这是怎么回事吗?
有两件事导致了“不允许”的问题:
Content-Type将标头设置为application/hal+json,这是它们唯一接受的内容。即使您计划对常规 JSON 数据做一些不同的事情,您也必须以某种方式解决这个问题(我没有这样做)X-CSRF-Token设置标头,您可以通过以下方式获取令牌/rest/session/token现在我不再收到“不允许”错误!不幸的是,因为身体需要hal+json,我现在收到一个"A fatal error occurred: Class does not exist"错误。