I have a Drupal 8 website. I need to access the list of articles using www.myexample.com/rest/views/articles.

I could get the token using curl --header "Content-type: application/json" -b cookies.tt --request POST "https://www.myexample.com/user/login?_format=json" --data '{"name":"myname", "pass":"mypassword"}'.

How do I use cUrl to pass the token or user/pass and get 'rest/views/articles' in JSON format?

I tried curl --header 'authorization: Bearer {D12G7AajU9NdUAb260zqwCimfdddR1Krg5}' --url https://www.myexample/rest/views/artigos/format?_format=json, but it returns an error.


I follow the @Ismail Cherri suggestion, seems a progress in the question but give me back :

"message":"The used authentication method is not allowed on this route."

In drupal website the config :

/entity/view/{view} is set to :

formats: hal_json, json
authentication: basic_auth

and /entity/entity_view_mode/{entity_view_mode} is set to :

formats: hal_json, json
authentication: basic_auth

anyone could help?

  • I updated my answer – Ismail Cherri Sep 14 '18 at 7:51
  • At /user/login you start a loggedin session and receive csrf token - this token needs for a POST/PATCH (with header key 'X-CSRF-Token') and this not eliminate the needs of Authorization - it is not needed for GET, I would recommend you to check JWT or api_key modules for D8 to use for authorization. Of course, a cookie authentication is the easiest way for an authorized request, but it is tricky to get a cookie (or data to create a cookie) if your frontend is not served from the address where is your rest point. – svetlio Sep 14 '18 at 9:01

First of all, does your endpoint require authentication to begin with? If yes, then use this https://www.drupal.org/docs/8/core/modules/rest/3-post-for-creating-content-entities as a guide though it is for a POST request.

You need to know which authentication method are you using, e.g. basic_auth, cookie, Oauth, etc?
According to the method, you should formulate your request. The use of X-CSRF-Token is for Cookie Authentication.
It might be in your case that you successfully got the token, but you're using it on an endpoint which requires Basic Auth.
As I see in your posted REST configuration, you're actually using Basic Auth, thus your request should look something like this:

curl --request GET \
  --url http://example.com/entity/view/{view} \
  --header 'authorization: Basic HASH_HERE' \
  --header 'content-type: application/json' \
|improve this answer|||||
  • Thanks so much! Your answer has been very usefull therefore this method give me back another message: "The used authentication method is not allowed on this route." in drupal config rules I set to allow, in navigator works well. Could you help me one more time ? – Orthophilos Sep 13 '18 at 10:23
  • Sure no problem, can you update your question to reflect the current state. Please add the update below your current questions so that others can follow along. Thanks :) – Ismail Cherri Sep 13 '18 at 10:50
  • Done! Could you follow your explanation? – Orthophilos Sep 13 '18 at 21:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.