Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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 = ""; 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: ./ 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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992639]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-03-20 01:05 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.