CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help with API call

 



kwatts59
Novice

Aug 30, 2018, 6:55 AM

Post #1 of 5 (826 views)
Help with API call Can't Post

Hi all,
I am trying to log into our Specimen database and pulling some data out using PERL. I think there is some syntax error somewhere. Below is the code:


Code
#!/usr/bin/perl 

use warnings;
use strict;
use Term::ReadKey;
use REST::Client;

print "Username: ";
chomp(my $user = <>);
print "Password: ";
ReadMode('noecho'); # don't echo
chomp(my $password = <>);
ReadMode(0); # back to normal
print "\n";

my $url = "https://openspecimendev.winship.emory.edu/openspecimen/rest/ng/sessions";
my $body = qq({ "loginName":"$user","password":"$password","domainName":"Emory"});

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;

#The basic use case
my $client = REST::Client->new();
$client->POST($url,$body);
my $response1 = $client->responseContent();
my $response2 = $client->responseCode();
#$response = $client->responseHeader();
print "Response (POST): $response1\n";
print "Response (POST): $response2\n";

$url = "https://openspecimendev.winship.emory.edu/openspecimen/rest/ng/specimens/11403";
$client->GET($url);
$response1 = $client->responseContent();
$response2 = $client->responseCode();
print "Response (GET): $response1\n";
print "Response (GET): $response2\n";


print "Finished\n";


Below is the output from the code. Basically it says the login credentials are invalid.

Code
Response (POST):  
Response (POST): 415
Response (GET): <html><head><title>Apache Tomcat/7.0.61 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - You must supply valid credentials to access the OpenSpecimen REST API</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>You must supply valid credentials to access the OpenSpecimen REST API</u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.61</h3></body></html>
Response (GET): 401
Finished


If anyone can provide any information as to what I am doing wrong, it would be greatly appreciated.
Thanks.


(This post was edited by kwatts59 on Aug 30, 2018, 10:45 AM)


FishMonger
Veteran / Moderator

Aug 30, 2018, 10:08 AM

Post #2 of 5 (817 views)
Re: [kwatts59] Help with API call [In reply to] Can't Post


Code
require 'conn_api.pl'; #username and password are saved in a separate file for security

Putting the username and password in that separate file/script does not add security. It adds obfuscation.

As the saying goes:

Quote
Security thru obscurity equals no security.



Code
$body = "{ \"loginName\":\"$user\",\"password\":\"$password\",\"domainName\":\"Emory\"}";

Why are you doing all that unnecessary escaping of the quotes?

If you use one of the other quoting methods, you won't need to do any escaping.

Code
$body = qq({ "loginName":"$user","password":"$password","domainName":"Emory"});


I haven't run any tests, but I doubt that you need to use any of those double quotes.

Code
$body = qq({ loginName:$user,password:$password,domainName:Emory});


When I pull up that url in the browser, it prompts for username and password but not domainName. Are you sure you need to pass that info?

The error message you're getting is telling you that you didn't supply valid login credentials. Are you able to login from a browser?

EDIT
I forgot to mention, you should ALWAYS include the warings and strict pragmas in your script.

Code
use warnings; 
use strict;



(This post was edited by FishMonger on Aug 30, 2018, 10:12 AM)


kwatts59
Novice

Aug 30, 2018, 11:05 AM

Post #3 of 5 (812 views)
Re: [FishMonger] Help with API call [In reply to] Can't Post

Thank you for the suggestions and your timely response. I made the changes to the PERL script as you suggested but the error messages are the same.

I tested the login from the example on this webpage:
https://openspecimen.atlassian.net/wiki/spaces/CAT/pages/1116035/REST+APIs

I definitely need to specify a domain, and I get a Bad Request error when I do not use double quotes.


(This post was edited by kwatts59 on Aug 30, 2018, 11:07 AM)
Attachments: openspecimen logn.png (14.4 KB)
  REST_API_test.png (42.5 KB)
  REST_API_bad_request.png (48.0 KB)


kwatts59
Novice

Sep 5, 2018, 9:41 AM

Post #4 of 5 (775 views)
Re: [kwatts59] Help with API call [In reply to] Can't Post

All,
For those of you who need to do API calls, I figured out the problem. The API call could not understand the value in the $body variable. To fix the problem, the following line of code has to be added before the $client->POST($url,$body); statement.


Code
$client->addHeader('Content-Type','application/json');


Problem solved.


(This post was edited by kwatts59 on Sep 5, 2018, 9:44 AM)


Zhris
Enthusiast

Sep 11, 2018, 7:29 AM

Post #5 of 5 (675 views)
Re: [kwatts59] Help with API call [In reply to] Can't Post

Your issue is specific to the API you are using. Although APIs should follow standards, they don't have to, having to provide a content type is by no means mandatory in all cases. The solution is to always read the documentation:

https://developer.atlassian.com/server/jira/platform/rest-apis/

Quote
Make sure the content type in the request is set to application/json, as shown in the example.


Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives