authenticity_token and post request
Added by asker asker over 13 years ago
Hello.
I need to create new issue at some server, but file attachment isnt realized in rest api yet, so I am using pure java and httpclient. I havent access to redmine logs and dont know ruby. :)
HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(server + "logout"); httpClient.executeMethod(postMethod); System.out.println(httpClient.executeMethod(postMethod)); // 302 System.out.println(postMethod.getResponseBodyAsString()); // You are being redirected. postMethod.releaseConnection(); Parser parser = new Parser(server + "login"); HasAttributeFilter attrFilter = new HasAttributeFilter("name", "authenticity_token"); NodeList nodeList = parser.parse(attrFilter); SimpleNodeIterator iter = nodeList.elements(); String authToken = null; while (iter.hasMoreNodes()) { Node node = iter.nextNode(); System.out.println(node.getText()); // input with token String[] nodeContent = node.getText().split(" "); for (String s : nodeContent) { if (s.contains("value")) authToken = s.substring(s.indexOf("\"") + 1, s.length() - 1); } } if (authToken != null) System.out.println(authToken); // token postMethod = new PostMethod(server + "login"); Part[] parts = new Part[5]; parts[0] = new StringPart("authenticity_token", authToken); parts[1] = new StringPart("back_url", server); parts[2] = new StringPart("username", username); parts[3] = new StringPart("password", password); parts[4] = new StringPart("autologin", "1"); postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); System.out.println(httpClient.executeMethod(postMethod)); // 500 System.out.println(postMethod.getResponseBodyAsString()); // Invalid form authenticity token. postMethod.releaseConnection();
Invalid form authenticity token. - wtf? why?
I have only one idea - ruby not correct handle encoding. But all experiments were unsuccessful. :(
Excuse me for broken english. :)