Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal login via Rest server

I'm developing a website who uses an external Drupal for the articles and pages. The purpose is to show the articles in a website using just html/css/js.

I've added an Rest Server module to the drupal back-end so I can do http requests for retreiving the articles. Now retreiving the articles from the drupal back-end works (see code below). Restdrupal is the name of my site and restendpoint is the name of the Rest server's endpoint (Captian Obvious)

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

Now I want my customer to be able to add some articles, so I need to login first. I've been searching the internet for days now and tried a million things but nothing worked for me. The latest thing i've 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've also changed the PUT into POST...

The response i'm getting is (no mather what I do) the same :

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

Could please somebody help me? Kind regards, Ceetn

like image 410
Ceetn Avatar asked Dec 16 '11 14:12

Ceetn


People also ask

Does Drupal have a REST API?

The RESTful Web Services API is new in Drupal 8. For each REST resource, you can specify the supported verbs, and for each verb, you can specify the serialization formats & authentication mechanisms.

Which RESTful authentication type uses a public API resource that allows anyone to make a request?

OAuth 2.0. OAuth (specifically, OAuth 2.0) is considered a gold standard when it comes to REST API authentication, especially in enterprise scenarios involving sophisticated web and mobile applications.


1 Answers

You have to enable application/x-www-form-urlencoded content type of your service endpoint.

Do as follows: Services -> Edit Resources -> select tab "Server" -> enable "application/x-www-form-urlencoded" and that's it

like image 121
Duong Avatar answered Sep 23 '22 01:09

Duong