cancel
Showing results for 
Search instead for 
Did you mean: 

POST request for "http://localhost:8080/alfresco/service/api/upload" resulted in 401 (Unauthorized); invoking error handler

kp3642
Champ in-the-making
Champ in-the-making

Hello , 

I am trying to upload a file on alfresco repo via LoginTicket. 

I am getting alfTicket Properly. but then it says    " POST request for "http://localhost:8080/alfresco/service/api/upload" resulted in 401 (Unauthorized); invoking error handler"



public class FileUpload {

      private static String getAlfticket() throws IOException, JSONException {
      String ticket = "";
      URL url = new URL("http://localhost:8080/alfresco/s/api/login?u=admin&pw=admin&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);
       
ticket =getData.getJSONObject("data").get("ticket").toString();
return ticket;
}



public static void main(String[] args) {

String ticketAlf=null;
try {
ticketAlf = FileUpload.getAlfticket(); // Getting a ticketAlf via custom method
} catch (JSONException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
String url = "http://localhost:8080/alfresco/service/api/upload"; // URL of API that needs to be called !!!

File file = new File("D:/anrose.txt"); //The file that needs to be uploaded !!

MultiValueMap<String, Object> request = new LinkedMultiValueMap<String, Object>();
FileSystemResource rsc = new FileSystemResource("D:/anrose.txt");

request.add("filedata", rsc);
request.add("siteid", "google");
request.add("containerid", "documentLibrary");
request.add("uploaddirectory", "test");
request.add("alf_ticket","TICKET_5e1bff6214cd28abf179d5398a64ec89d83a2c0a");


RestTemplate rest = new RestTemplate();

rest.postForEntity(url, request, String.class); // Here it gives the error
}

}

I guess it's the problem with alfresco Login ticket. 

Can anyone please help me ? 

Thanks in advance. 

1 REPLY 1

afaust
Legendary Innovator
Legendary Innovator

alf_ticket always needs to be provided as an URL parameter, not as content in the request body.