4 Replies Latest reply: Sep 17, 2012 6:58 PM by user8742475 RSS

    REST tutorial

    user8742475
      I need to call a REST service and I want to use Java JDK.

      Below is login info from the 3rd party vendor given to me.

      URL: http://servername/rest/user/login
      HTTP Request Type: POST
      Parameters: username, password
      Cookie Required: False

      The response will be a JSON-encoded object.

      How do I call this REST service and parse the response back? Do you have any tutorial that I can review? I'm new to REST.

      Thanks.
        • 1. Re: REST tutorial
          r035198x
          Here is an example of how to create a RESTful webservices and call it from a client: http://docs.oracle.com/javaee/6/tutorial/doc/gkoib.html
          • 2. Re: REST tutorial
            user8742475
            Below is my code

            String urlParameters = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("password1", "UTF-8");

            URL url = new URL("http://servername/rest/user/login");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setInstanceFollowRedirects(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("charset", "utf-8");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setUseCaches (false);

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
            wr.writeBytes(urlParameters);

            // Get the response
            BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            line = rd.readLine();
            while (line != null) {
            System.out.println(line);
            line = rd.readLine();
            }

            wr.flush();
            wr.close();
            connection.disconnect();

            But I got the below errors back

            java.io.IOException: Server returned HTTP response code: 401 for URL: http://servername/rest/user/login
                 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
                 at client.Class1.login(Class1.java:31)
                 at client.Class1.main(Class1.java:130)


            Do you have any idea?

            Thanks
            • 3. Re: REST tutorial
              jtahlborn
              user8742475 wrote:
              But I got the below errors back

              java.io.IOException: Server returned HTTP response code: 401 for URL: http://servername/rest/user/login
                   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
                   at client.Class1.login(Class1.java:31)
                   at client.Class1.main(Class1.java:130)


              Do you have any idea?
              do you know what a "401" http status code means? do you know what the REST service expects the login content to be?
              • 4. Re: REST tutorial
                user8742475
                Thanks. I got past the security error (401 error) and now I got the 406 error. Below is my code. For some reason, it does not recognize the JSON object returned. Do you have any ideas? Thanks

                URL url = new URL("http://servername/rest/user/login");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);

                //Set basic authentication in the header
                String userPassword = "admin:password123";
                String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
                conn.setRequestProperty("Authorization", "Basic " + encoding);

                //Submit user and password. A JSON object will return that contains session name and session id for this user
                String formParameters = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("user1password", "UTF-8");

                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty("Content-Length", Integer.toString(formParameters.length()));
                conn.setRequestProperty("Accept", "application/json");

                DataOutputStream out = new DataOutputStream(conn.getOutputStream());

                out.writeBytes(formParameters);
                out.flush();
                out.close();

                System.out.println(conn.getResponseCode());