How to send REST api get and post request to server and get response from server in java

REST API Get request
 import java.io.*;  
 import java.net.HttpURLConnection;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 public class Restapigetrequest {  
   public void requestresponse() {  
     String output = "";  
     String requesturl="http://httpbin.org/get";  
     try {  
       // sent get request to sever  
       URL url = new URL(requesturl);  
       HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
 /*  
      // Add header if need  
       conn.setRequestMethod("GET");  
       conn.setRequestProperty("Content-Type", "application/json");  
       conn.setRequestProperty("token", "da6e387d-8abc-4f88-bf21-ad37deff042e");  
  */  
       // get response from server and print response  
       if (conn.getResponseCode() != 200) {  
         System.out.println(requesturl + " -> Fail " + conn.getResponseCode());  
       } else {  
         BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));  
         while ((output = br.readLine()) != null) {  
           System.out.println(output);  
         }  
         conn.disconnect();  
       }  
     } catch (MalformedURLException e) {  
       e.printStackTrace();  
     } catch (IOException e) {  
       e.printStackTrace();  
     }  
   }  
   public static void main(String[] a) throws IOException {  
     Restapigetrequest at = new Restapigetrequest();  
     at.requestresponse();  
   }  
 }  
Output:
REST API POST request
 import java.io.BufferedReader;  
 import java.io.InputStreamReader;  
 import org.apache.http.HttpResponse;  
 import org.apache.http.client.HttpClient;  
 import org.apache.http.client.methods.HttpPost;  
 import org.apache.http.entity.StringEntity;  
 import org.apache.http.impl.client.DefaultHttpClient;  
 import org.json.simple.JSONArray;  
 import org.json.simple.JSONObject;  
 public class Restapipostrequest {  
   // Generate json string  
   public static String jsonString() {  
  /*  
   {  
   "text": "Generally, these two terms,  
   "title": "test-450",  
   "Id":4869  
  }  
  */  
     JSONObject obj1 = new JSONObject();  
     obj1.put("text", "Generally, these two terms");  
     obj1.put("title", "Idea created by script");  
     obj1.put("Id", 82);  
     System.out.println(obj1.toJSONString());  
     return obj1.toJSONString();  
   }  
   public static void find_contact() {  
     HttpClient httpClient = new DefaultHttpClient();  
     String postcall ="http://httpbin.org/post";  
     try {  
       //you will need api key here!!  
       HttpPost request = new HttpPost(postcall);  
 /*  
                // Add header if need  
       request.addHeader("Content-Type", "application/json");  
       request.addHeader("api_token", "68c93d98-4ce2-4d5b-8a8c-cad25e0ab113");  
       // Add json input if need  
       StringEntity params = new StringEntity(jsonString());  
       request.setEntity(params);  
 */  
       HttpResponse response = httpClient.execute(request);  
       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
       String result = "";  
       String line;  
       while ((line = rd.readLine()) != null) {  
         result += line;  
       }  
       System.out.println(result);  
       rd.close();  
       System.out.println("status:" + response.getStatusLine());  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
   public static void main(String args[]) {  
     find_contact();  
   }  
 }  

No comments:

Post a Comment