mojo::useragent and Get Request Parameters - perl

Let's Say I have this Mojo::UserAgent get request :
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
print $ua->get('https://google.com?q=mojolicious&format=json');
on the above example the get parameters provided as part of the url itself , I am asking if there are an option to separate the request parameters from the url
I tried form but its not achieving the same result like using this directly as url 'https://google.com?q=mojolicious&format=json'
print $ua->get('https://google.com' => form => {q= > 'mojolicious' ,format='json'});
any idea how to achieve the above ?

Your code has some formatting issues:
q= > 'mojolicious should read q => 'mojolicious'
format='json' should read format => 'json'
and you are missing a closing curly }
So all in all your line should look like this:
$ua->get('https://google.com' => form => {q => 'mojolicious', format => 'json' });
This method returns an instance Mojo::Transaction::HTTP which you can use like this:
my $tx = $ua->get('https://google.com' => form => { q => 'mojolicious', format => 'json' });
print $tx->res->body;
For further reading please consult https://metacpan.org/pod/Mojo::Transaction::HTTP

Related

Debugging HTTP::Tiny

I am using HTTP::Tiny to interact with rest api below an example :
my $req = $ua->get('https://myapi.com/user?xyz', {headers =>
{token => 'xyzzz',
data => '5343'.
}});
i am using Data::Dumper to see the response header and body
print Dumper($req); -#i can see the response
is there an option to see also the HTTP request i am sending using HTTP::Tiny ,without the need of other tool like wireshark etc..
something like what mojo::useragent achieve with MOJO_CLIENT_DEBUG=1 any idea?
I don't see such a feature mentioned in the HTTP::Tiny documentation, nor do I see it facilitated in the source code for the module. However, you can use a module such as Test::MockModule to get a closer look at what is going on. Here is an example:
#!/usr/bin/env perl
use strict;
use warnings;
use HTTP::Tiny;
use Test::MockModule;
use Data::Dumper;
my $t = HTTP::Tiny->new();
my $mock = Test::MockModule->new('HTTP::Tiny');
$mock->redefine('_request' => sub {
warn "In _request: ", Dumper {self => $_[0], method => $_[1], url => $_[2], args => $_[3]};
return $mock->original('_request')->(#_);
});
print "Response: ", Dumper $t->get('http://localhost:3000');
So in this snippet we are mocking _request, but the mock method we install in behalf of _request calls out to the original _request method, so our mocked method becomes a wrapper around the original method, and we can dump the underlying object and args passed into the method. I'm doing the dump prior to the request being made, but I could have done the initial dump, captured the return value of the real request, and then dump the object again afterward if I thought it might contain additional useful information. So long as we return the actual response, the calling code is none the wiser.
Here's an example of the output. The URL I'm hitting is just a default Mojolicious::Lite app.
In _request: $VAR1 = {
'method' => 'GET',
'self' => bless( {
'agent' => 'HTTP-Tiny/0.076',
'no_proxy' => [],
'max_redirect' => 5,
'keep_alive' => 1,
'verify_SSL' => 0,
'timeout' => 60
}, 'HTTP::Tiny' ),
'args' => {},
'url' => 'http://localhost:3000'
};
Response: $VAR1 = {
'headers' => {
'content-length' => '146',
'content-type' => 'text/html;charset=UTF-8',
'server' => 'Mojolicious (Perl)',
'date' => 'Sun, 26 May 2019 03:19:57 GMT'
},
'protocol' => 'HTTP/1.1',
'reason' => 'OK',
'url' => 'http://localhost:3000',
'success' => 1,
'status' => '200',
'content' => '<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body><h1>Welcome to the Mojolicious real-time web framework!</h1>
</body>
</html>
'
};
Before I was able to know that _request was where I wanted to target my mock, I had to look at the source code for HTTP::Tiny. Fortunately the ::Tiny part of the module means there's not a whole lot to look at. It's really a relatively simple module.
You may decide that for your own purposes it makes more sense to introduce your wrapper at some other point in HTTP::Tiny, but wrapping _request seems like a pretty good choice for most cases.

Sending Very Specific SOAP::Lite requests

My company uses has a lot of internal APIs that use very specific header and formatting requirements. I am new to SOAP::Lite and I'm trying to make it work within the company's framework.
Try #1:
Ideally, I would like to be able to just take the raw XML template (see bottom of the post), populate some placeholder variables, and send it to the endpoint using the following code:
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $reply = $client->InquireEnterpriseOrderDataRequest($rawxml);
However, this results in my header and request sections being enclosed in it's own "envelope", "body" and "InquireEnterpriseOrderDataRequest" which is rejected by the service.
Try #2:
The next thing I tried was to break my request into two pieces: header and request and use SOAP::Data and SOAP::Header to send those:
my $rawxmlheader = '<ns2:MessageHeader xmlns:ns2="http://mycompany.com/MessageHeader.xsd" xmlns="http://mycompany.com/CingularDataModel.xsd">
<ns2:TrackingMessageHeader>
<version>111</version>
<originalVersion/>
<messageId/>
<originatorId>ABC</originatorId>
<responseTo/>
<returnURL/>
<timeToLive>360000</timeToLive>
<conversationId>9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0</conversationId>
<routingRegionOverride/>
<dateTimeStamp>2017-05-12T12:47:53Z</dateTimeStamp>
<uniqueTransactionId>mytransid</uniqueTransactionId>
</ns2:TrackingMessageHeader>
<ns2:SecurityMessageHeader>
<userName>myusername</userName>
<userPassword>mypass</userPassword>
</ns2:SecurityMessageHeader>
<ns2:SequenceMessageHeader>
<sequenceNumber/>
<totalInSequence/>
</ns2:SequenceMessageHeader>
</ns2:MessageHeader>';
my $rawxmlrequest = '<OrderSearchCriteria>
<OrderDetails>
<SearchByOrderAction>
<orderActionNumber>12345654</orderActionNumber>
<orderActionVersion>1</orderActionVersion>
</SearchByOrderAction>
</OrderDetails>
</OrderSearchCriteria>
<provisioningDetailsIndicator>true</provisioningDetailsIndicator>';
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $header = SOAP::Header->type('xml' => $rawxmlheader);
my $elem = SOAP::Data->type('xml' => $rawxmlrequest);
my #arguments;
push(#arguments, $header);
push(#arguments, $elem);
my $reply = $client->InquireEnterpriseOrderDataRequest(#arguments);
This produced a very similar request to what was needed with the exception that the InquireEnterpriseOrderDataRequest blob did not contain the xsi:schemaLocation, xmlns or xmlns:xsi values that seem to be required.
Try #3:
Now I was grasping at straws, so I also tried to granularly create my own XML using something like this:
my $temp_elements =
SOAP::Data->name("OrderSearchCriteria" => \SOAP::Data->value(
SOAP::Data->name("OrderDetails" => \SOAP::Data->value(
SOAP::Data->name("SearchByOrderAction" => \SOAP::Data->value(
SOAP::Data->name("orderActionNumber" => '301496944'),
SOAP::Data->name("orderActionVersion" => '3')
)
)
)
))
)->type("SomeObject");
my $client = SOAP::Lite->new( proxy => "$serviceURL");
my $reply = $client->InquireEnterpriseOrderDataRequest($temp_elements);
The problem here was that I don't know how to include the xsi:schemaLocation, xmlns and xmlns:xsi values or prepend the header information.
Of course, I'd like to go with the simplest possible implementation but any suggestions are appreciated! Thanks in advance!
Required request format:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns2:MessageHeader xmlns:ns2="http://mycompany.com/MessageHeader.xsd" xmlns="http://mycompany.com/CingularDataModel.xsd">
<ns2:TrackingMessageHeader>
<version>111</version>
<originalVersion/>
<messageId/>
<originatorId>ABC</originatorId>
<responseTo/>
<returnURL/>
<timeToLive>360000</timeToLive>
<conversationId>9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0</conversationId>
<routingRegionOverride/>
<dateTimeStamp>2017-04-11T18:47:53Z</dateTimeStamp>
<uniqueTransactionId>mytransid</uniqueTransactionId>
</ns2:TrackingMessageHeader>
<ns2:SecurityMessageHeader>
<userName>myusername</userName>
<userPassword>mypass</userPassword>
</ns2:SecurityMessageHeader>
<ns2:SequenceMessageHeader>
<sequenceNumber/>
<totalInSequence/>
</ns2:SequenceMessageHeader>
</ns2:MessageHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<InquireEnterpriseOrderDataRequest xsi:schemaLocation="http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd" xmlns="http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OrderSearchCriteria>
<OrderDetails>
<SearchByOrderAction>
<orderActionNumber>12345654</orderActionNumber>
<orderActionVersion>1</orderActionVersion>
</SearchByOrderAction>
</OrderDetails>
</OrderSearchCriteria>
<provisioningDetailsIndicator>true</provisioningDetailsIndicator>
</InquireEnterpriseOrderDataRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This should generate the required response using SOAP::Lite request.
use strict;
use warnings;
use SOAP::Lite +trace=>'all';
$on_action = '';
$proxy = 'http://serviceURL';
$soap = SOAP::Lite->new(proxy => $proxy);
$soap->on_action(sub {$on_action});
$soap->readable(1);
$soap->autotype(0);
$soap->serializer->register_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd' => 'xsi');
$soap->serializer->register_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd' => 'xsi:schemaLocation');
$soap->default_ns('http://mycompany.com/InquireEnterpriseOrderDataRequest.xsd');
$soap->envprefix('SOAP-ENV');
$sheader = SOAP::Header->name(MessageHeader =>\SOAP::Header->value(SOAP::Header->name(TrackingMessageHeader => \SOAP::Header->value(
SOAP::Header->name(version => 111),
SOAP::Header->name(originalVersion => ''),
SOAP::Header->name(messageId => ''),
SOAP::Header->name(originatorId => 'ABC'),
SOAP::Header->name(responseTo => ''),
SOAP::Header->name(returnURL => ''),
SOAP::Header->name(timetoLive => 360000),
SOAP::Header->name(conversationId => '9AF0E9281A524262980F5284F4C57888_CCE423E277C74FA9A84D2155CD612EB3_0'),
SOAP::Header->name(routingRegionOverride => ''),
SOAP::Header->name(dateTimeStamp => '2017-04-11T18:47:53Z'),
SOAP::Header->name(timetoLive => 'mytransid'),
))->prefix('ns2')))->attr({'xmlns:ns2' => 'http://mycompany.com/MessageHeader.xsd',xmlns => 'http://mycompany.com/CingularDataModel.xsd'})->prefix('ns2');
push #request,(
SOAP::Data->name(OrderSearchCriteria => \SOAP::Data->value(
SOAP::Data->name(OrderDetails => \SOAP::Data->value(
SOAP::Data->name(SearchByOrderAction => \SOAP::Data->value(
SOAP::Data->name(orderActionNumber => 12345654),
SOAP::Data->name(orderActionVersion => 1),
)))))));
$reply = $soap->InquireEnterpriseOrderDataRequest($sheader,#request);

I can't connect using an api

I'm quite new to API's so I don't know if this should be more straight forward.
I write the following perl script
use strict;
use LWP::UserAgent;
require HTTP::Request;
my $request = HTTP::Request->new(GET => 'http://api.elsevier.com/content/ev/results?apiKey=1234&query=stress&database=c&updateNumber=1&pageSize=1');
my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);
then when I get my response and print it in the debugger I get the following
HTTP::Response=HASH(0x9aedff8)
'_content' => '{"service-error":{"status":{"statusCode":"AUTHENTICATION_ERROR","statusText":"Requestor configuration settings insufficient for access to this resource."}}}'
'_headers' => HTTP::Headers=HASH(0x9aedfe8)
'allow' => 'GET'
'client-date' => 'Wed, 29 Mar 2017 08:08:25 GMT'
'client-peer' => '198.185.19.118:80'
'client-response-num' => 1
'content-length' => 156
'content-type' => 'application/json;charset=UTF-8'
'date' => 'Wed, 29 Mar 2017 08:08:24 GMT'
'p3p' => 'CP="IDC DSP LAW ADM DEV TAI PSA PSD IVA IVD CON HIS TEL OUR DEL SAM OTR IND OTC"'
'server' => 'api.elsevier.com 9999'
'vary' => 'Origin'
'x-cnection' => 'close'
'x-els-apikey' => 'e688c9db4db0386581dbe4c4dda46164'
'x-els-reqid' => '0000015b190d89fe-a0d0'
'x-els-status' => 'AUTHENTICATION_ERROR(Requestor configuration settings insufficient for access to this resource.)'
'x-els-transid' => 'cbf787b4-d171-4e35-8237-8cab3c931205'
'x-re-ref' => '1 1490774904423414'
'_msg' => 'Forbidden'
'_protocol' => 'HTTP/1.1'
'_rc' => 403
'_request' => HTTP::Request=HASH(0x9fc3000)
'_content' => ''
'_headers' => HTTP::Headers=HASH(0x9ae73e0)
'user-agent' => 'libwww-perl/5.831'
'_method' => 'GET'
'_uri' => URI::http=SCALAR(0x9e25188)
-> 'http://api.elsevier.com/content/ev/results?apiKey=e688c9db4db0386581dbe4c4dda46164&query=stress&database=c&updateNumber=1&pageSize=1'
'_uri_canonical' => URI::http=SCALAR(0x9e25188)
-> REUSED_ADDRESS
one of the notable lines is
x-els-status' => 'AUTHENTICATION_ERROR(Requestor configuration settings insufficient for access to this resource.)'
I don't know how to get a proper response text. I tried searching their websites for examples, but I can't seem to get it. as well I'm not sure if the key is only for scopus but not engineering village which I'm trying to use.
There website is here. https://dev.elsevier.com/index.html?utm_expid=89327795-0.AtRZzToKQ2u1mZEyQ3n7OQ.0&utm_referrer=https%3A%2F%2Fdev.elsevier.com%2Ftecdoc_ev_retrieval_request.html
any help would be appreciated
To get the text out of your response, you need to call the $response->decoded_content method. That will give you the JSON string that you can see in _content in your debug output. I've indented it to make it easier to read.
{
"service-error" : {
"status" : {
"statusCode" : "AUTHENTICATION_ERROR",
"statusText" : "Requestor configuration settings insufficient for access to this resource."
}
}
}
You can use the JSON module to decode this into a Perl data structure.
use JSON 'from_json';
my $res = $ua->request($req);
my $json = from_json( $res->decoded_content );
The error message you get back clearly states that you are not authenticated properly. I've looked at this guide from the documentation you mentioned. It seems that the apiKey URL param works, if you have the right type of account. You should check with whoever made that account for you, or if that was you and you're not sure, the account manager at that service that is working with you. They'll tell you if you are using the right API key, and if this method of authentication works for you.
Since this API also offers to use a custom header X-ELS-APIKey: [apikey] for the authentication I would suggest using that. Your API key is a secret, and you shouldn't share it with anyone. It's like a password. If you put it into the URL, it might show up in log files. But as a header, it does usually not.
This is how you add a custom header to an HTTP request. Make sure you don't have the apiKey URL param any more if you do this.
my $req = HTTP::Request->new( GET => $url ); # no apiKey=123 here!
$req->header( 'X-ELS-APIKey' => 123 );
Now as a last step, you should check the HTTP response code of the response. A 200 (or most other codes that start with 2) means the request was successful. The 403 that you are getting back means unauthorized, which also hints at that you are not authenticated correctly.
Since it seems that this API returns JSON in both success and failure cases, you might need to decode it for both. If you care to examine the failure response, that makes sense. If not, you can skip that part. To do this, use $res->is_success, which is also used in the synopsis of the LWP::UserAgent documentation.
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request;
use JSON 'from_json';
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => 'http://api.elsevier.com/content/ev/results?query=stress&database=c&updateNumber=1&pageSize=1' );
$req->header( 'X-ELS-APIKey' => 123 );
if ($req->is_success) {
my $json = from_json( $res->decoded_content );
# ... do stuff with the response
} else {
# something went wrong
}

Post local image file on tumblr via API with perl

I've been messing around with the tumblr API with perl and have gotten several functions to work.
However, I can not get local image files to upload via perl.
Here is my code that works for URLs
use LWP::Authen::OAuth;
use JSON;
use Data::Dumper;
use strict;
my $ua = LWP::Authen::OAuth->new(
oauth_consumer_key => 'xxx',
oauth_consumer_secret => 'xxx',
oauth_token => 'xxx',
oauth_token_secret => 'xxx',
);
my $response;
$response = $ua->post( 'http://api.tumblr.com/v2/blog/mytumblr.tumblr.com/post', [
type => 'photo',
url => 'http://www.example.com/mypic.jpg' ,
caption => 'Test image 1',
]);
if ($response->is_success) {
print "it worked";
}
else {
print "it did not work \n \n \n \n";
print $response->as_string;
}
However, when i substitute "url" for "data" in the post parameters (as instructed in their API description here - http://www.tumblr.com/docs/en/api/v2#posting), I keep getting an error response from tumblr. I have tried several ways of entering the "data" parameter - as a path to the file, as a binary representation, as a URL encoded binary representation, as a url encoded base64 binary representation, stuck ech one of those values as a sole element in an array - I have tried all, and with each one I get a error message back from tumblr.
So, can someone please show me how to upload a local image file to tumblr?
I'm not entirely familiar with the tumblr API, but a quick googling found me this example: https://gist.github.com/derekg/1198576
I would try
$response = $ua->post( 'http://api.tumblr.com/v2/blog/mytumblr.tumblr.com/post', [
type => 'photo',
'data[0]' => $file_contents , ## LWP::Useragent should automatically urlencode this
caption => 'Test image 1',
]);
According to this answer https://stackoverflow.com/a/177866/810448, it's possible that "data[]" would also work in this situation.
I would also consider adding 'Content-type: application/x-www-form-urlencoded' to the request headers, if LWP::Useragent is not doing it already.

Is it possible to conditionally pass options to a method in perl?

Similar to this question regarding Ruby, I'd like to conditionally pass parameters to a method. Currently, I have it configured as follows:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
from => $from,
until => $until,
);
What I'd like is to be able to conditionally pass the from and/or until parameters, depending on previous code. This is not syntactically correct, but something like this:
from => $from if ($from),
until => $until if ($until),
or this:
if ($from) {from => $from,}
if ($until) {until => $until,}
Is this possible, and if so, how would I go about doing it?
You could use the ternary ?: operator with list operands:
my $recs = $harvester->listAllRecords(
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
$from ? (from => $from) : (),
$until ? (until => $until) : (),
);
It may also be worth knowing about the "conditional list include" pseudo-operator, which in this case would work like
...
(from => $from) x !!$from,
(until => $until) x !!defined($until),
...
but the ternary operator expression is probably easier to read for most people.
The other option is to build a list (or hash) of args and then call the method:
my %args = (
metadataPrefix => 'marc21',
metadataHandler => 'MARC::File::SAX',
set => 'pd',
);
$args{from} = $from if $from;
$args{until} = $until if $until;
my $recs = $harvester->listAllRecords(%args);