0

I would like to POST a node to REST API on my Drupal site. On that site, I have enabled the following Web services modules:

  • HAL
  • HTTP Basic Authentication
  • REST UI
  • RESTful Web Services
  • Serialization

The configuration of REST UI looks like this: enter image description here

My code is based on this Guzzle example:

$url = 'http://my-site/node?_format=hal_json';
$auth = ['admin', 'password'];
$body = json_encode([
  'title' => [['value' => '2']],
  'field_bar' => [['value' => 'bar']],
  'type' => [['target_id' => 'bar']],
  '_links' => ['type' => [
    'href' => 'http://my-site/rest/type/node/bar'
  ]],
]);
$headers = [
  'Content-Type' => 'application/hal+json',
  'X-CSRF-Token' => 'my-token',
];

try {
  $response = \Drupal::httpClient()
    ->post($url, ['auth' => $auth, 'body' => $body, 'headers' => $headers]);
  $code = $response->getStatusCode();
  if ($code == 200) {
    \Drupal::messenger()->addMessage($this->t('Success!'));
  }
  else {
    \Drupal::messenger()->addMessage($this->t('Bad response!'), 'error');
  }
}
catch (RequestException $e) {
  \Drupal::messenger()->addMessage($this->t('Foo'), 'error');
  \Drupal::messenger()->addMessage(Psr7\str($e->getRequest()));
  if ($e->hasResponse()) {
    \Drupal::messenger()->addMessage(Psr7\str($e->getResponse()));
  }
  watchdog_exception('foo', $e);
}

When it runs, it throws:

No route found for the specified format hal_json. Supported formats: html.

Full message:

POST /node?_format=hal_json HTTP/1.1 User-Agent: Drupal/8.6.3 (+https://www.drupal.org/) GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.1.14 Authorization: Basic some-string== Host: my-site Content-Type: application/hal+json X-CSRF-Token: my-token {"title":[{"value":"2"}],"field_bar":[{"value":"bar"}],"type":[{"target_id":"bar"}],"_links":{"type":{"href":"http://my-site/rest/type/node/bar"}}}

HTTP/1.1 406 Not Acceptable Cache-Control: must-revalidate, no-cache, private Content-Language: en Content-Type: application/hal+json Date: Sun, 25 Nov 2018 11:19:16 GMT Expires: Sun, 19 Nov 1978 05:00:00 GMT Link: https://my-site/node?_format=html; rel="alternate"; type="text/html" Strict-Transport-Security: max-age=0 Vary: X-Content-Type-Options: nosniff X-Debug-Info: some-string== X-Frame-Options: SAMEORIGIN X-Generator: Drupal 8 (https://www.drupal.org) X-Platform-Cache: MISS X-Platform-Cluster: some-string X-Platform-Processor: some-string X-Platform-Router: some-string X-Request-Id: some-string X-Robots-Tag: noindex, nofollow X-Ua-Compatible: IE=edge Content-Length: 88 {"message":"No route found for the specified format hal_json. Supported formats: html."}

Note: A cURL from the command line works.

Any ideas? Thanks.

  • I don’t know much about this module, but could it be that the route endpoint does not define more than the default format of html even if the UI says it does? – Kevin Nov 25 '18 at 18:55
  • Even if I configure manually by importing my own configuration, I still get the same error. – 24ma13wg Nov 25 '18 at 22:24
  • I opened up my CORS settings and it's working now. – 24ma13wg Nov 26 '18 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.