Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Overview

This page provides sample code for creating REST clients for various popular programming languages.

 

 

1. REST client code sample in JVM using JAVA

 

-In this tutorial, we show how to create a RESTful Java client with Java built-in HTTP client library. It’s simple to use and good enough to perform basic operations for REST service.

 Java client to send a “POST” request to PAGA getTransactionDetails method on the merchant restful API, with json string will be as simple as below.
REST Client
package restfulclient.main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
import java.security.MessageDigest;
public class RestClient {
 
	public static void main(String[] args) {
		 
		try {
			 
			URL url = new URL("http://www.mypaga.com/paga-webservices/merchant-rest/getTransactionDetails");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("POST");
				conn.setDoOutput(true);
				
				conn.setRequestProperty("Content-Type", "application/json");
				conn.setRequestProperty("principal", "AA6EEB78-BD92-4D0B-A87D-E3DB46C53B70"); //business organization publicId
				conn.setRequestProperty("credentials", "password"); //business organization password
				StringBuilder sBuilder = new StringBuilder();
				sBuilder.append("09012939283"); //referenceNumber param value
				sBuilder.append("0123"); //business organization api-key
				
				conn.setRequestProperty("hash", RestClientTest.hashSHA512(sBuilder.toString(), 1000, true).toString());
				//input should be the data formatted as a JSON.
				String input = "{\"referenceNumber\":\"09012939283\"}"; 
		 
				OutputStream os = conn.getOutputStream();
				os.write(input.getBytes());
				os.close();
	 
			if (conn.getResponseCode() != 201 && conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
					+ conn.getResponseCode());
			}
	 
			BufferedReader br = new BufferedReader(new InputStreamReader(
					(conn.getInputStream())));
	 
			String output;
			System.out.println("Output from Server .... \n");
			while ((output = br.readLine()) != null) {
				System.out.println(output);
			}
	 
			conn.disconnect();
	 
		  } catch (MalformedURLException e) {
	 
			e.printStackTrace();
	 
		  } catch (IOException e) {
	 
			e.printStackTrace();
	 
		 }
	}
	//hash sha-512 with some iterations
	public static Object hashSHA512(String message, int iterations, boolean returnHex) {
	
		try {
		
			MessageDigest mda = MessageDigest.getInstance("SHA-512");
		
			byte [] digest = mda.digest(message.getBytes());
		
			for(int i = 0; i < iterations - 1; i++) {
			
				digest = mda.digest(digest);
			}
	
			if(returnHex) {
				return new String(Hex.encodeHex(digest));
			} else {
				return digest;
			}
		
		} catch(Exception e) {
			return null;
		}
	}
}

 

  • No labels