Saturday 12 November 2022

perl rest client crud tutorial example using REST::Client


Hi,Today  Perl script that demonstrates how to interact with a REST API using the REST::Client module. The script shows examples of performing HTTP requests such as GET, POST, and DELETE to interact with different endpoints of a RESTful API. The script starts by importing necessary modules, and then sets up basic configurations for authentication and API endpoint URLs. The code defines three subroutines, each performing a specific type of API request: GetList(), CreatePost(), and DeleteList(). These subroutines use the HTTP methods of the REST::Client module to call the respective API endpoints, and they return the API response. Finally, the script calls these subroutines and prints the API responses on the console.


# packages ####################################### #REST CLIENT API ENDPOINTS EXAMPLE #Author@kaavannan.k\@solution4u.com ####################################### use REST::Client; use MIME::Base64; use JSON; use Data::Dumper; #The basic use case Rest cient my $client = REST::Client->new(); #TO-DO load all this configuration from configuaration file like YAML, JSON my $auth_req_url = 'https://xxxxxxxxxx'; my $base_url = 'https://xxxxxxxxxxx'; my $auth_data = "{ \"accessKeyId\": "xxxxxxxxxxx", \"accessKeySecret\":\"xxxxxxxxxxx\"}"; # Autherization request $client->POST($auth_req_url, $auth_data, { "Content-type" => 'application/json'}); my $response = from_json ($client->responseContent()); my $access_token = $response->{'access_token'}; sub GetList { #============================ #GET() Method for Callinglist #============================ my $endpoint = shift; my $headers = {Accept => 'application/json', Authorization => 'Bearer ' . $access_token}; $client -> GET($base_url . $endpoint, $headers); print $client->responseContent(), "\n"; } sub CreatePost { my $body_data= { "listName" => "test11", "externalIdColumn" => "Ref", "customer1Column" => "ID", "destinationMappings" => [ { "fieldName"=> "PhoneNumber", "fieldValue"=> "PhoneNumber1" } ], "FieldMappings" => [ { "fieldName"=> "AccountNumber", "fieldValue" => "AccountNumber" }, { "fieldName" => "PhoneNum2", "fieldValue" => "PhoneNumber2" }, { "fieldName" => "PhoneNum3", "fieldValue" => "PhoneNumber3" }, { "fieldName" => "PhoneNum4", "fieldValue" => "PhoneNumber4" }, { "fieldName" => "PhoneNum5", "fieldValue" => "PhoneNumber5" }, { "fieldName" => "PhoneNum6", "fieldValue" => "PhoneNumber6" } ] }; my $endpoint="/lists/"; my $req_header = { 'Authorization' => 'Bearer ' . $access_token, 'content-Type'=> 'application/json', 'Accept' => 'application/json, text/javascript, */*'}; # Calling server $client->POST( $base_url . $endpoint, encode_json $body_data, $req_header ); if ( $client->responseCode() eq '200' ) { print "Response Content : " . $client->responseContent() . "\n"; return from_json ($client->responseContent())->{'Id'}; } else { print "Response Code : " . $client->responseCode()."\n"; foreach ( $client -> responseHeaders() ) { print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n+"; } return undef; } } sub PostList { #=========================================== #POST() Method for Upload call-list details from csv file #=========================================== $req_body{Id} = shift; $req_body{Id2} = shift; $req_body{TimeZone} = shift; $req_body{expirationDate} = shift; $req_body{fileName} = shift; $endpoint = "/lists/$Id/upload"; $req_body{'File'} = encode_base64 $file_content; # header formation my $upl_header = { 'Authorization' => 'Bearer ' . $access_token, 'content-Type'=> 'application/json', 'Accept' => 'application/json, text/javascript, */*; q=0.01'}; # Calling server $client->POST( $base_url . $endpoint, encode_json \%req_body, $upl_header ); if ( $client->responseCode() eq '200' || $client->responseCode() eq '202' ) { print "Response Code : " . $client->responseCode(); } else { print "Response Code : " . $client->responseCode()."\n"; foreach ( $client -> responseHeaders() ) { print 'Header: ' . $_ . '=' . $client->responseHeader($_) . "\n+"; } print "\n"; } print "\nResponse : " . $client->responseContent() . "\n"; } sub DeleteList { #============================ #DELETE() Method for list #============================ my $endpoint = shift; my $headers = {Accept => 'application/json', Authorization => 'Bearer ' . $access_token}; $client -> DELETE($base_url . $endpoint, $headers); print $client->responseContent(), "\n"; } #function calling print "\n-------- Create list --------\n"; $id = CreatePost(); print "\n------- Getting list detail by id -------\n"; GetList("/lists/$id"); #Using list ID to fetch list print "\n ------- upload file -------\n"; PostList($Id, 242, 'GMT', true, '30/07/2021', false, 'data.csv'); print "\n-------- Delete list --------\n"; $id = DeleteList();




Labels: , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home