Drupal login via Rest server

2019-02-09 19:25发布

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

3条回答
姐就是有狂的资本
2楼-- · 2019-02-09 19:49

Found the solution myself. 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
    }
});
查看更多
Emotional °昔
3楼-- · 2019-02-09 19:50

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-02-09 20:04

might need to enable that parser type?

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

查看更多
登录 后发表回答