Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Proxying web API requests

by davido (Archbishop)
on Sep 09, 2012 at 23:39 UTC ( #992639=note: print w/ replies, xml ) Need Help??


in reply to Proxying web API requests

I don't know what your authentication scheme is for the 3rd party, but the process ought to be pretty simple using Mojolicious:

use Mojolicious::Lite; # Hit PerlMonks. If a node ID is supplied, fetch it. get '/*resource' => { resource => '' } => sub { my $self = shift; my $uri = "http://www.perlmonks.org/"; my $resource = $self->stash('resource'); $uri .= length $resource ? "?node_id=$resource" : ''; $self->ua->get( $uri => sub { my ($ua, $tx) = @_; $self->render( text => $tx->res->dom->html ); }); }; app->start;

Fire it up: ./myapp.pl daemon. Then hit http://localhost:3000. You'll get the Monastery Gates. Now try http://localhost:3000/992634, and you'll fetch this node.

This basic example doesn't exercise any care in processing the resource request. That's up to you. And if you need to modify some aspect of the incoming request, the level of difficulty depends on what needs altering. A simple change to the restful request is easy, though. And of course you're not limited to listening on port 3000, but you can read about that in the docs.

In case you blinked and missed it, this starts up a daemon that listens on a port for http requests. When a request comes in as a 'get', it adds a new domain to the URL, but passes through the resource params to that URL. Then the built-in user agent fetches that resource, and the renderer renders it back to the client as HTML.

I'd encourage you to watch the mojocast that discusses using the user-agent. It demonstrates using SSL, constructing transations, and a lot more. And for help in dealing with placeholders in your routes, this mojocast.


Dave


Comment on Re: Proxying web API requests
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992639]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-08-29 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (284 votes), past polls