Rest API call can be performed using any of the following 4 methods:
3. PHP Code
4. Java Code
cURL Commands
- For performing the REST API Call, incorporate the URL for the API within the call.
- For enhanced security and to safeguard the connection from external intrusion, we require the Access Token as a part of Header to prove identity.
- For your reference, we have included a sample call below that incorporates all essential elements :
-
curl -v -X GET http://api-uat.statestreet.com:9001/accounts/v1 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <Access-Token>"
-
- Once the Access Token is verified, you'd get a response from our servers.
Python Code
- For performing the REST API Call, incorporate the URL for the API within the call.
- For enhanced security and to safeguard the connection from external intrusion, we require the Access Token as a part of the script to prove identity.
- For your reference, we have included a sample call below that incorporates all essential elements :
-
def getResponseFromAPI(url,accessToken): request = urllib2.Request(url) request.add_header('Authorization', 'Bearer '+accessToken) request.add_header('Accept',"application/json") request.get_method = lambda: 'GET' response = urllib2.urlopen(request).read() print (response)
-
- Once the Access Token is verified, you'd get a response from our servers.
PHP Code
- For performing the REST API Call, incorporate the URL for the API within the call.
- For enhanced security and to safeguard the connection from external intrusion, we require the Access Token as a part of the script to prove identity.
- For your reference, we have included a sample call below that incorporates all essential elements :
-
<?php function getAuthToken($url,$client_app_key,$client_app_secret){ $data = array('grant_type' => 'client_credentials'); $options = array( 'http' => array( 'header' => array ('Content-type: application/x-www-form-urlencoded', 'Accept: application/json', 'Authorization: Basic '.base64_encode("$client_app_key:$client_app_secret")) 'method' => 'POST', 'content' => http_build_query($data) ) ); $context = stream_context_create($options); $result = file_get_contents($url , false, $context); if ($result === FALSE) { /* Handle error */ } $data=json_decode($result, true); return $data['access_token']; } function getApiResponse($url,$authToken){ $options = array( 'http' => array( 'header' => array ('Content-type: application/x-www-form-urlencoded', 'Authorization: Bearer '.$authToken), 'method' => 'GET') ); $context = stream_context_create($options); $result = file_get_contents($url , false, $context); return $result; } $url = 'http://api-uat.statestreet.com:9001'; $authToken= getAuthToken($url.'/oauth2/token',$client_app_key,$client_app_secret); echo getApiResponse($url."/your-api-path",$authToken); ?>
-
- Once the Access Token is verified, you'd get a response from our servers.
Java Code
- For performing the REST API Call, incorporate the URL for the API within the call.
- For enhanced security and to safeguard the connection from external intrusion, we require the Access Token as a part of the script to prove identity.
- For your reference, we have included a sample call below that incorporates all essential elements :
-
private static String getAPiResponse(String URL,String authToken) throws IOException { URL obj = new URL(URL); String authHeaderValue = "Bearer " + authToken; HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("Authorization", authHeaderValue); con.setRequestProperty("Accept", "application/json"); con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("GET Response Code :: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { //success BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } else { System.out.println("GET request not worked"); throw new IOException("GET request not worked"); } }
-
- Once the Access Token is verified, you'd get a response from our servers.