Drupal 8 Jsonapi Get 请求抛出错误请求错误
Drupal 8 Jsonapi Get request throws bad request error
使用 json api 设置 drupal 网站 API,当访问 link 以获取资源 http://example.com/jsonapi/node/article - 出现错误
"title": "Bad Request",
"status": "400",
"detail": "The following query parameters violate the JSON:API spec: 'q'.",
您的网络服务器似乎重写了请求 URL。特别是它添加了一个 q
查询参数。 Drupal 返回的完整错误消息包括应用程序在第一个 errors
对象的 links.via
路径下接收的完整 URL:http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle
请参阅上面的完整响应。
这可能是由于用于为 Drupal 提供服务的 Web 服务器配置错误造成的。特别是重写规则似乎是错误的。已报告类似问题 in this bug。
我建议您将 Web 服务器配置与 default .htaccess
provided. If you are using nginx, you might want to have a look at an example configuration for nginx 进行比较。
$ curl http://207.148.125.64/jsonapi/node/article | json_pp
{
"errors" : [
{
"title" : "Bad Request",
"status" : "400",
"meta" : {
"exception" : "Drupal\Core\Http\Exception\CacheableBadRequestHttpException: The following query parameters violate the JSON:API spec: 'q'. in /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php:78\nStack trace:\n#0 /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php(36): Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator->validateQueryParams(Object(Symfony\Component\HttpFoundation\Request))\n#1 [internal function]: Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator->onRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#2 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#3 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(127): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))\n#4 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)\n#5 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#6 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#7 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#8 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#9 /var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php(45): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#10 /var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#11 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#12 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#13 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#14 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#15 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#16 {main}",
"trace" : [
{
"class" : "Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator",
"type" : "->",
"line" : 36,
"file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
"args" : [
{
"server" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"request" : {},
"attributes" : {},
"files" : {}
}
],
"function" : "validateQueryParams"
},
{
"class" : "Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator",
"type" : "->",
"args" : [
{},
"kernel.request",
{
"_serviceId" : "event_dispatcher"
}
],
"function" : "onRequest"
},
{
"file" : "/var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php",
"args" : [
[
{
"_serviceId" : "jsonapi.custom_query_parameter_names_validator.subscriber"
},
"onRequest"
],
{},
"kernel.request",
{
"_serviceId" : "event_dispatcher"
}
],
"function" : "call_user_func",
"line" : 111
},
{
"class" : "Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher",
"type" : "->",
"line" : 127,
"args" : [
"kernel.request",
{}
],
"function" : "dispatch",
"file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php"
},
{
"file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php",
"args" : [
{
"attributes" : {},
"files" : {},
"request" : {},
"server" : {},
"headers" : {},
"query" : {},
"cookies" : {}
},
1
],
"function" : "handleRaw",
"class" : "Symfony\Component\HttpKernel\HttpKernel",
"type" : "->",
"line" : 68
},
{
"args" : [
{
"request" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"function" : "handle",
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php",
"class" : "Symfony\Component\HttpKernel\HttpKernel",
"line" : 57,
"type" : "->"
},
{
"line" : 47,
"type" : "->",
"class" : "Drupal\Core\StackMiddleware\Session",
"function" : "handle",
"args" : [
{
"server" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"request" : {},
"attributes" : {},
"files" : {}
},
1,
true
],
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php"
},
{
"args" : [
{
"files" : {},
"attributes" : {},
"request" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"server" : {}
},
1,
true
],
"function" : "handle",
"file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
"class" : "Drupal\Core\StackMiddleware\KernelPreHandle",
"type" : "->",
"line" : 106
},
{
"class" : "Drupal\page_cache\StackMiddleware\PageCache",
"type" : "->",
"line" : 85,
"file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
"args" : [
{
"files" : {},
"attributes" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"request" : {}
},
1,
true
],
"function" : "pass"
},
{
"file" : "/var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php",
"args" : [
{
"server" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"request" : {},
"attributes" : {},
"files" : {}
},
1,
true
],
"function" : "handle",
"class" : "Drupal\page_cache\StackMiddleware\PageCache",
"type" : "->",
"line" : 45
},
{
"file" : "/var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php",
"function" : "handle",
"args" : [
{
"files" : {},
"attributes" : {},
"request" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {}
},
1,
true
],
"type" : "->",
"line" : 49,
"class" : "Drupal\jsonapi\StackMiddleware\FormatSetter"
},
{
"class" : "Asm89\Stack\Cors",
"line" : 47,
"type" : "->",
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php",
"args" : [
{
"request" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"function" : "handle"
},
{
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php",
"function" : "handle",
"args" : [
{
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"request" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"line" : 52,
"type" : "->",
"class" : "Drupal\Core\StackMiddleware\ReverseProxyMiddleware"
},
{
"file" : "/var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php",
"function" : "handle",
"args" : [
{
"request" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"type" : "->",
"line" : 23,
"class" : "Drupal\Core\StackMiddleware\NegotiationMiddleware"
},
{
"file" : "/var/www/html/core/lib/Drupal/Core/DrupalKernel.php",
"args" : [
{
"attributes" : {},
"files" : {},
"server" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"request" : {}
},
1,
true
],
"function" : "handle",
"class" : "Stack\StackedHttpKernel",
"line" : 693,
"type" : "->"
},
{
"line" : 19,
"type" : "->",
"class" : "Drupal\Core\DrupalKernel",
"function" : "handle",
"args" : [
{
"attributes" : {},
"files" : {},
"request" : {},
"server" : {},
"cookies" : {},
"headers" : {},
"query" : {}
}
],
"file" : "/var/www/html/index.php"
}
]
},
"detail" : "The following query parameters violate the JSON:API spec: 'q'.",
"source" : {
"file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
"line" : 78
},
"links" : {
"info" : {
"href" : "http://jsonapi.org/format/#query-parameters"
},
"via" : {
"href" : "http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle"
}
}
}
],
"jsonapi" : {
"version" : "1.0",
"meta" : {
"links" : {
"self" : {
"href" : "http://jsonapi.org/format/1.0/"
}
}
}
}
}
使用 json api 设置 drupal 网站 API,当访问 link 以获取资源 http://example.com/jsonapi/node/article - 出现错误
"title": "Bad Request", "status": "400", "detail": "The following query parameters violate the JSON:API spec: 'q'.",
您的网络服务器似乎重写了请求 URL。特别是它添加了一个 q
查询参数。 Drupal 返回的完整错误消息包括应用程序在第一个 errors
对象的 links.via
路径下接收的完整 URL:http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle
请参阅上面的完整响应。
这可能是由于用于为 Drupal 提供服务的 Web 服务器配置错误造成的。特别是重写规则似乎是错误的。已报告类似问题 in this bug。
我建议您将 Web 服务器配置与 default .htaccess
provided. If you are using nginx, you might want to have a look at an example configuration for nginx 进行比较。
$ curl http://207.148.125.64/jsonapi/node/article | json_pp
{
"errors" : [
{
"title" : "Bad Request",
"status" : "400",
"meta" : {
"exception" : "Drupal\Core\Http\Exception\CacheableBadRequestHttpException: The following query parameters violate the JSON:API spec: 'q'. in /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php:78\nStack trace:\n#0 /var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php(36): Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator->validateQueryParams(Object(Symfony\Component\HttpFoundation\Request))\n#1 [internal function]: Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator->onRequest(Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#2 /var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\GetResponseEvent), 'kernel.request', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))\n#3 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(127): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.request', Object(Symfony\Component\HttpKernel\Event\GetResponseEvent))\n#4 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)\n#5 /var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#6 /var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#7 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#8 /var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#9 /var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php(45): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#10 /var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\jsonapi\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#11 /var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#12 /var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#13 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#14 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)\n#15 /var/www/html/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))\n#16 {main}",
"trace" : [
{
"class" : "Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator",
"type" : "->",
"line" : 36,
"file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
"args" : [
{
"server" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"request" : {},
"attributes" : {},
"files" : {}
}
],
"function" : "validateQueryParams"
},
{
"class" : "Drupal\jsonapi\EventSubscriber\JsonApiRequestValidator",
"type" : "->",
"args" : [
{},
"kernel.request",
{
"_serviceId" : "event_dispatcher"
}
],
"function" : "onRequest"
},
{
"file" : "/var/www/html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php",
"args" : [
[
{
"_serviceId" : "jsonapi.custom_query_parameter_names_validator.subscriber"
},
"onRequest"
],
{},
"kernel.request",
{
"_serviceId" : "event_dispatcher"
}
],
"function" : "call_user_func",
"line" : 111
},
{
"class" : "Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher",
"type" : "->",
"line" : 127,
"args" : [
"kernel.request",
{}
],
"function" : "dispatch",
"file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php"
},
{
"file" : "/var/www/html/vendor/symfony/http-kernel/HttpKernel.php",
"args" : [
{
"attributes" : {},
"files" : {},
"request" : {},
"server" : {},
"headers" : {},
"query" : {},
"cookies" : {}
},
1
],
"function" : "handleRaw",
"class" : "Symfony\Component\HttpKernel\HttpKernel",
"type" : "->",
"line" : 68
},
{
"args" : [
{
"request" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"function" : "handle",
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/Session.php",
"class" : "Symfony\Component\HttpKernel\HttpKernel",
"line" : 57,
"type" : "->"
},
{
"line" : 47,
"type" : "->",
"class" : "Drupal\Core\StackMiddleware\Session",
"function" : "handle",
"args" : [
{
"server" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"request" : {},
"attributes" : {},
"files" : {}
},
1,
true
],
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php"
},
{
"args" : [
{
"files" : {},
"attributes" : {},
"request" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"server" : {}
},
1,
true
],
"function" : "handle",
"file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
"class" : "Drupal\Core\StackMiddleware\KernelPreHandle",
"type" : "->",
"line" : 106
},
{
"class" : "Drupal\page_cache\StackMiddleware\PageCache",
"type" : "->",
"line" : 85,
"file" : "/var/www/html/core/modules/page_cache/src/StackMiddleware/PageCache.php",
"args" : [
{
"files" : {},
"attributes" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"request" : {}
},
1,
true
],
"function" : "pass"
},
{
"file" : "/var/www/html/modules/contrib/jsonapi/src/StackMiddleware/FormatSetter.php",
"args" : [
{
"server" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"request" : {},
"attributes" : {},
"files" : {}
},
1,
true
],
"function" : "handle",
"class" : "Drupal\page_cache\StackMiddleware\PageCache",
"type" : "->",
"line" : 45
},
{
"file" : "/var/www/html/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php",
"function" : "handle",
"args" : [
{
"files" : {},
"attributes" : {},
"request" : {},
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {}
},
1,
true
],
"type" : "->",
"line" : 49,
"class" : "Drupal\jsonapi\StackMiddleware\FormatSetter"
},
{
"class" : "Asm89\Stack\Cors",
"line" : 47,
"type" : "->",
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php",
"args" : [
{
"request" : {},
"headers" : {},
"cookies" : {},
"query" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"function" : "handle"
},
{
"file" : "/var/www/html/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php",
"function" : "handle",
"args" : [
{
"query" : {},
"headers" : {},
"cookies" : {},
"server" : {},
"request" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"line" : 52,
"type" : "->",
"class" : "Drupal\Core\StackMiddleware\ReverseProxyMiddleware"
},
{
"file" : "/var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php",
"function" : "handle",
"args" : [
{
"request" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"server" : {},
"files" : {},
"attributes" : {}
},
1,
true
],
"type" : "->",
"line" : 23,
"class" : "Drupal\Core\StackMiddleware\NegotiationMiddleware"
},
{
"file" : "/var/www/html/core/lib/Drupal/Core/DrupalKernel.php",
"args" : [
{
"attributes" : {},
"files" : {},
"server" : {},
"headers" : {},
"query" : {},
"cookies" : {},
"request" : {}
},
1,
true
],
"function" : "handle",
"class" : "Stack\StackedHttpKernel",
"line" : 693,
"type" : "->"
},
{
"line" : 19,
"type" : "->",
"class" : "Drupal\Core\DrupalKernel",
"function" : "handle",
"args" : [
{
"attributes" : {},
"files" : {},
"request" : {},
"server" : {},
"cookies" : {},
"headers" : {},
"query" : {}
}
],
"file" : "/var/www/html/index.php"
}
]
},
"detail" : "The following query parameters violate the JSON:API spec: 'q'.",
"source" : {
"file" : "/var/www/html/modules/contrib/jsonapi/src/EventSubscriber/JsonApiRequestValidator.php",
"line" : 78
},
"links" : {
"info" : {
"href" : "http://jsonapi.org/format/#query-parameters"
},
"via" : {
"href" : "http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle"
}
}
}
],
"jsonapi" : {
"version" : "1.0",
"meta" : {
"links" : {
"self" : {
"href" : "http://jsonapi.org/format/1.0/"
}
}
}
}
}