【问题标题】:Drupal 8 Jsonapi Get request throws bad request errorDrupal 8 Jsonapi Get 请求抛出错误请求错误
【发布时间】:2019-12-02 06:47:50
【问题描述】:

使用 json api 设置 drupal 网站 API,当访问获取资源 http://example.com/jsonapi/node/article 的链接时 - 出现错误

"title": "错误请求", “状态”:“400”, "detail": "以下查询参数违反 JSON:API 规范:'q'。",

【问题讨论】:

  • 请包括您的请求,包括查询参数和标头。
  • 通过浏览器访问了该链接。在安装 jsonapi 模块之前它工作了。
  • 错误信息表示请求中有q查询参数。有必要知道它是否是一个错误的请求,或者该查询参数是否以某种方式自动添加。
  • 这是实际链接-“example.com/jsonapi/node/article”,查询参数是自动添加的。
  • 我很确定这不是完整的 URL。它缺少协议,example.com 不是您可以拥有的域。如果不提供更多信息,很难理解发生了什么。请包含使用curlfetch 的复制品以及已安装的drupal 模块列表。

标签: drupal drupal-8 json-api


【解决方案1】:

您的网络服务器似乎重写了请求 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 进行比较。如果你使用的是 nginx,你可能想看看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/"
            }
         }
      }
   }
}

【讨论】:

  • 如果你能接受我的回答,如果它解决了你的问题,那就太好了。
  • 我做不到。
猜你喜欢
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多