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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Aug 30, 2018, 6:55 AM

Post #1 of 5 (1760 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:


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 = "";
my $body = qq({ "loginName":"$user","password":"$password","domainName":"Emory"});


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

$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.

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;} {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

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

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

Veteran / Moderator

Aug 30, 2018, 10:08 AM

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

require ''; #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:

Security thru obscurity equals no security.

$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.

$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.

$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?

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

use warnings; 
use strict;

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


Aug 30, 2018, 11:05 AM

Post #3 of 5 (1746 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:

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)


Sep 5, 2018, 9:41 AM

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

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.


Problem solved.

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


Sep 11, 2018, 7:29 AM

Post #5 of 5 (1609 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:

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



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

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