Alfresco login api giving Bad Request 400

放肆的年华 提交于 2019-12-13 03:33:44

问题


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"

回答1:


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
});



回答2:


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



来源:https://stackoverflow.com/questions/26399016/alfresco-login-api-giving-bad-request-400

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!