Alfresco login api giving Bad Request 400

2019-09-01 07:22发布

I am trying to login to alfresco through api. I do not know why it is returning error 400 Bad Request. The json should be correct and in my ajax call I have also set the content type to 'application/json'.

This is my ajax call.

var jsonData = JSON.stringify({ username : usernameV, password : passwordV });

var request = $.ajax({
    settings : {contentType:'application/json'},
    type: "POST",
    url: "http://---ip---/alfresco/service/api/login",
    data: jsonData
});

The json string in the console.

{"username":"admin","password":"admin1"} 

Error

400 Bad Request 
Request sent by the client was syntactically incorrect

Message in responseJSON object

Unable to parse JSON POST body: A JSONObject text must begin with '{' at character 0"

2条回答
我只想做你的唯一
2楼-- · 2019-09-01 07:51

I have created one java program which was doing same thing.I think you should pass username and password in url.Even if you directly hit below url in browser, It will give you alf_ticket, which is use full in authentication for alfresco.

private static String getAlfticket() throws IOException, JSONException {
        String ticket = "";
        URL url = new URL("http://hostname/alfresco/service/api/login u="+USERNAME+"&pw="+PASSWORD+"&format=json");
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        String encoding = con.getContentEncoding();
        encoding = encoding == null ? "UTF-8" : encoding;
        String json = IOUtils.toString(in, encoding);
        JSONObject getData = new JSONObject(json);
        System.out.println(getData.getJSONObject("data").get("ticket")
                .toString());
        ticket =getData.getJSONObject("data").get("ticket").toString();
        return ticket;
} 


Krutik Jayswal
Alfresco Developer

查看更多
冷血范
3楼-- · 2019-09-01 07:59

I suspect this is to do with the way you are setting the contentType as the only ways for this to occur appear to either be empty JSON or an incorrect contentType. Try:

var request = $.ajax({
    contentType:"application/json",
    type: "POST",
    url: "http://---ip---/alfresco/service/api/login",
    data: jsonData
});
查看更多
登录 后发表回答