When accessing examples/rest-client-actions, the following exception is thrown.
GuzzleHttp\Exception\ClientException: Client error: `GET http://localhost:8888/drupal9/rest/node` resulted in a `406 Not Acceptable` response: <!DOCTYPE html> <html lang="zh-hans" dir="ltr"> <head> <meta charset="utf-8" /> <meta name="Generator" content="Dr (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).
GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object) (Line: 204)
GuzzleHttp\Promise\Promise::callHandler(1, Object, NULL) (Line: 153)
GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() (Line: 48)
GuzzleHttp\Promise\TaskQueue->run(1) (Line: 248)
GuzzleHttp\Promise\Promise->invokeWaitFn() (Line: 224)
GuzzleHttp\Promise\Promise->waitIfPending() (Line: 269)
GuzzleHttp\Promise\Promise->invokeWaitList() (Line: 226)
GuzzleHttp\Promise\Promise->waitIfPending() (Line: 62)
GuzzleHttp\Promise\Promise->wait() (Line: 182)
GuzzleHttp\Client->request('GET', 'http://localhost:8888/drupal9/rest/node', Array) (Line: 108)
Drupal\rest_example\RestExampleClientCalls->index() (Line: 63)
Drupal\rest_example\Controller\RestExampleClientController->indexAction()
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Original report
When navigating to examples/rest-client-actions, the following error appears:
The website encountered an unexpected error. Please try again later.
Error: Call to a member function get() on null in Drupal\rest_example\Controller\RestExampleClientController->indexAction() (line 54 of modules/contrib/examples/modules/rest_example/src/Controller/RestExampleClientController.php).
Drupal\rest_example\Controller\RestExampleClientController->indexAction()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 57)
Drupal\Core\StackMiddleware\Session->handle() (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
Stack\StackedHttpKernel->handle() (Line: 705)
Drupal\Core\DrupalKernel->handle() (Line: 19)
Comments
Comment #2
hgneng commentedI got the same issue.
And I think this reason is that configFactory is not a member of ControllerBase (it's a member of FormBase). So we should initialize it in constructor.
However, after I fix the issue of configFactory, I get another one:
Comment #3
jungleReproduced!
Comment #4
jaydee1818 commentedI'm also experiencing this
Comment #6
avpadernoComment #7
avpadernoI fixed the access to the not initialized
$configFactoryin #3458249: Do not use ControllerBase as parent class for RestExampleClientController.Comment #8
jvogt commentedApparently this has to do with Drupal dropping support for the Accept header, which this module still uses. Instead, we must set the _format query string argument.
Example: https://example.com/rest/node?_format=json
I've attached a patch that addresses the issue for GET calls in RestExampleClientCalls.php. Note that it doesn't make changes to other HTTP method calls (POST, DELETE, etc). It also doesn't make the controller render correctly for '/examples/rest-client-actions'. That has a bunch of other issues, but at least you'll no longer get the 406 error.
Comment #10
avpadernoTests are failing because #3468407: Add the missing @group annotation to test classes. The other GitLab CI jobs are not failing, so the provided patch is good to go.
Comment #12
avpadernoThank you for the patch! The merge request has been merged.