Kalyan S
Kalyan S

Reputation: 3

Drupal 8 Jsonapi Get request throws bad request error

Setting up drupal website API using json api, when accessing the link for get resource http://example.com/jsonapi/node/article - getting error

"title": "Bad Request", "status": "400", "detail": "The following query parameters violate the JSON:API spec: 'q'.",

Upvotes: 0

Views: 1814

Answers (1)

jelhan
jelhan

Reputation: 6338

Your web server seems to rewrite the request URL. In particular it adds a q query parameter. The full error message returned by Drupal includes the full URL that the application was receiving under links.via path of first errors object: http://207.148.125.64/jsonapi/node/article?q=%2Fjsonapi%2Fnode%2Farticle Please see above for full response.

This likely caused by a wrong configuration of web server used to serve Drupal. In particular the rewrite rule seems to be wrong. A similar issue has been reported in this bug.

I would recommend that you compare your web server configuration against the 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/"
            }
         }
      }
   }
}

Upvotes: 2

Related Questions