Monday, April 4, 2016

REST api client

This is a REST API client which behaves similar to any other REST clients like POSTMAN. As an example I am illustrating GET and POST request. In POST request I am reading JSON from file and feeding the JSON as inputstream to the connection object

This is the JSON which I am sending as InputStream



 package com.rest.client;  
 import java.io.BufferedInputStream;  
 import java.io.BufferedOutputStream;  
 import java.io.BufferedReader;  
 import java.io.BufferedWriter;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.io.OutputStream;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.net.URLConnection;  
 import org.apache.commons.io.IOUtils;  
 public class RESTclient {  
      private static final String ADDRESS = "http://localhost:9090/StudyProject/webapi/phonebooks";  
      public static void main(String[] args) {  
           /*  
            * HttpURLConnection connection = null; try { connection = GETclient();  
            * } catch (IOException e) { // TODO Auto-generated catch block  
            * e.printStackTrace(); } finally { connection.disconnect(); }  
            */  
           try {  
                POSTclient();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
      public static HttpURLConnection GETclient() throws IOException {  
           URL url = new URL(ADDRESS);  
           // using HTTP URL connection object  
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
           connection.setRequestMethod("GET");  
           connection.setRequestProperty("accept", "application/json");  
           int responseCode = connection.getResponseCode();  
           System.out.println(responseCode);  
           BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());  
           while (bis.available() > 0) {  
                char c = (char) bis.read();  
                System.out.print(c);  
           }  
           return connection;  
      }  
      // http://bernhardhaeussner.de/odd/json-escape/  
      public static void POSTclient() throws IOException {  
           URL url = new URL(ADDRESS);  
           HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
           connection.setDoOutput(true);  
           connection.setRequestMethod("POST");  
           connection.setRequestProperty("content-type", "application/json");  
           // String json = "";  
           BufferedInputStream inputStream = new BufferedInputStream(  
                     new FileInputStream(new File("/Users/S_Das/Desktop/user.json")));  
           byte[] json = IOUtils.toByteArray(inputStream);  
           OutputStream os = connection.getOutputStream();  
           // sending a byte stream  
           os.write(json);  
           os.flush();  
           BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());  
           while (bis.available() > 0) {  
                char c = (char) bis.read();  
                System.out.print(c);  
           }  
      }  
 }  

No comments:

Post a Comment