2

Could you, please, help me to write an appropriate HTTP POST request in JAVA to authenticate in YouTrack using their REST API? It should look like this (taken from YouTrack Documentation):

POST http://localhost:8081/rest/user/login Content-Length: 24 Connection: keep-alive Content-Type: application/x-www-form-urlencoded Cookie: login=root&password=root

I have written a below code to open connection, send a cookie and handle a server response:

            URL url = new URL(webPage); 
            URLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setDoOutput(true);
            urlc.setDoInput(true);
            ((HttpURLConnection) urlc).setRequestMethod("POST");
            urlc.setRequestProperty("Content-Length", Integer.toString(24));
            urlc.setRequestProperty("Connection", "keep-alive");
            urlc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            String myCookie = "login=root&password=root";
            urlc.setRequestProperty("Cookie", myCookie);

            InputStream is = urlc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb.append(charArray, 0, numCharsRead);
            }
            String result = sb.toString();

            System.out.println("result = " + result); 

But I'm getting HTTP response 400. Please tell me what am I doing wrong? I would be really grateful if you suggest a decision to the problem. Thank you!

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Oleksii
  • 31
  • 5
  • What does it look like when you're sending it? – kolossus Feb 23 '15 at 18:50
  • I'm getting the following text on console: java.io.IOException: Server returned HTTP response code: 400 for URL: server_address/rest/user/login at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at reporting.YouTrackApiActions$SSLTest.main(YouTrackApiActions.java:59) – Oleksii Feb 23 '15 at 18:52
  • Try the same request from within your browser, using any of the API testing tools available (e.g. RESTClient, POSTMan etc). Your code otherwise looks ok. You're probably not conforming to the api – kolossus Feb 23 '15 at 19:01
  • java:59 is a string where InputStream is was created – Oleksii Feb 23 '15 at 19:04
  • RESTClient appeared to be quite a nice plugin for that purpose. Unfortunately I'm getting the same error 400 from the server. I was offered to use curl requests as a workaround. – Oleksii Feb 24 '15 at 15:33
  • At least, you're now confident there's nothing wrong with your code; it's strictly an API challenge. – kolossus Feb 24 '15 at 16:01

1 Answers1

1

Login and password should be placed in the body of request, not in the cookie. Strange documentation muddled my brain. Here is the working solution made with the help of Apache libraries:

            HttpClient client = HttpClientBuilder.create().build();

            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addTextBody("login", "root").addTextBody("password", "root");

            HttpEntity entity = entityBuilder.build();

            HttpPost post = new HttpPost(webPage);
            post.setEntity(entity);

            HttpResponse response = client.execute(post);
Oleksii
  • 31
  • 5