0

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.

UPDATE

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 :

GET
formats: hal_json, json
authentication: basic_auth

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

GET
formats: hal_json, json
authentication: basic_auth

anyone could help?

2
  • 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
    Commented Sep 14, 2018 at 9:01
  • If you're just using basic_auth and nothing else, then you need to perform a basic auth login for every request you make. If you also enable cookie auth then you can use the cookie for subsequent requests after creating it with a call to user/login.
    – Lambic
    Commented Dec 7, 2021 at 15:25

1 Answer 1

0

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.


UPDATE
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' \
3
  • 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 ? Commented Sep 13, 2018 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 :) Commented Sep 13, 2018 at 10:50
  • Done! Could you follow your explanation? Commented Sep 13, 2018 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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