Logging into Drupal via Rest Server

I am developing a website that uses external Drupal for articles and pages. The goal is to display articles on a website using only html / css / js.

I added the Rest Server module to the backpen drupal so that I can execute HTTP requests to return articles. Now back to the articles from the back drupal works (see Code below). Restdrupal is the name of my site, and restendpoint is the name of the Rest (Captian Obvious) server endpoint

$.ajax({ url : "http://127.0.0.1/restdrupal/restendpoint/node.json", dataType : 'json', success : function(data) { //further code } }); 

Now I want my client to be able to add some articles, so I need to log in first. I searched the internet for several days and tried millions of things, but nothing worked for me. The last thing I tried (with jQuery) was this:

 $.ajax({ url : "http://127.0.0.1/restdrupal/restendpoint/user/login", dataType:'application/json', type : 'PUT', data : 'Name=myusername&Pass=mypassword', success : function(data) { //further code }, error:function(data){ //Error handling } }); 

I also changed PUT to POST ...

The answer I get is (not what I do) the same thing:

 406 Not Acceptable: Unsupported request content type application/x-www-form-urlencoded 

Can anyone help me? Regards, Ceetn

+6
source share
3 answers

You need to include the application content type / x -www-form-urlencoded service endpoint.

Do the following: Services → Change resources → select the “Server” tab → enable “application / x-www-form-urlencoded” and that it

+24
source

Found a solution on my own. For those who are interested:

 $.ajax({ url : "http://127.0.0.1/restdrupal/restpoint/user/login.json", type : 'post', data : 'username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), dataType : 'json', error : function(data) { //error code }, success : function(data) { //success code } }); 
+2
source

may need to enable this type of parser?

check out this link. maybe this will help you get some ideas https://drupal.stackexchange.com/questions/3207/simple-rest-request-to-create-nodes

+1
source

Source: https://habr.com/ru/post/903914/


All Articles