Acme-CPANModulesBundle-Import-MojoliciousAdvent-2017
view release on metacpan or search on metacpan
devdata/https_mojolicious.io_blog_2017_12_05_day-5-your-apps-built-in-commands view on Meta::CPAN
<p>You can fetch the Perl headlines from reddit.
To do so we fetch the url (following redirects with <code>-r</code>), then we give it a <a href="http://mojolicious.org/perldoc/Mojo/DOM/CSS">CSS3 selector</a>, and finally extract the text from each found element.</p>
<pre><code>mojo get -r reddit.com/r/perl 'p.title > a.title' text
</code></pre>
<p>How fun is that?!</p>
<ul>
<li>You can POST or PUT or DELETE data.</li>
<li>It handles HTTP basic authentication using <code>username:password@</code> in the URL.</li>
<li>You can submit forms, even with file uploads using the standard <code>@filename</code> syntax.</li>
<li>You can pipe data to the command if you just want to send the raw contents of a file rather than url-encode it.</li>
<li>See lots more examples in the <a href="http://mojolicious.org/perldoc/Mojolicious/Command/get#SYNOPSIS">documentation</a>.</li>
</ul>
<p>But I haven't even touched on its coolest feature yet.
This command also works on your application when you request a relative url.
This is so handy for debugging requests during rapid development; you don't even need a browser!</p>
devdata/https_mojolicious.io_blog_2017_12_08_day-8-mocking-a-rest-api view on Meta::CPAN
{ "ip": "10.0.0.2", "os": "Debian 8" }
]
</code></pre>
<p>And also one of the individual item endpoints:</p>
<pre><code>$ perl test-api.pl get /servers/1
{ "ip": "10.0.0.1", "os": "Debian 9" }
</code></pre>
<p>Currently we handle all request methods (<code>GET</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code>)
the same, but my API doesn't work like that. So, I need to be able to
provide different data for different request methods. To do that, let's add the
request method to the template path:</p>
<pre><code class="hljs"><span class="hljs-comment"># test-api.pl</span><span class="hljs-comment">
</span><span class="hljs-keyword">use</span> <span class="hljs-function">Mojolicious::Lite</span>;
any '<span class="hljs-string">/*path</span>' => <span class="hljs-keyword">sub </span>{
<span class="hljs-keyword">my</span> ( <span class="hljs-type">$c</span> ) = <span class="hljs-type">@_</span>;
<span class="hljs-keyword">return</span> <span class="hljs-type">$c</span>-><span class="hljs-type">render</span>(
template => <span class="hljs-function">join</span>( '<span class="hljs-string">/</span>', <span class="hljs-function">uc</span> <span class="hljs-type">$c</span>-><span class="hljs-type">req</span>-><span class="hljs-type">m...
devdata/https_mojolicious.io_blog_2017_12_08_day-8-mocking-a-rest-api view on Meta::CPAN
<p>And finally, since I'm using this to test an AJAX web application,
I need to allow the preflight <code>OPTIONS</code> request to succeed and I need to
make sure that all of the correct <code>Access-Control-*</code> headers are set
to allow for cross-origin requests.</p>
<pre><code class="hljs"><span class="hljs-comment"># test-api.pl</span><span class="hljs-comment">
</span><span class="hljs-keyword">use</span> <span class="hljs-function">Mojolicious::Lite</span>;
hook after_build_tx => <span class="hljs-keyword">sub </span>{
<span class="hljs-keyword">my</span> (<span class="hljs-type">$tx</span>, <span class="hljs-type">$app</span>) = <span class="hljs-type">@_</span>;
<span class="hljs-type">$tx</span>-><span class="hljs-type">res</span>-><span class="hljs-type">headers</span>-><span class="hljs-type">header</span>( '<span class="hljs-string">Access-Control-Allow-Origin</span>' => '<spa...
<span class="hljs-type">$tx</span>-><span class="hljs-type">res</span>-><span class="hljs-type">headers</span>-><span class="hljs-type">header</span>( '<span class="hljs-string">Access-Control-Allow-Methods</span>' => '<sp...
<span class="hljs-type">$tx</span>-><span class="hljs-type">res</span>-><span class="hljs-type">headers</span>-><span class="hljs-type">header</span>( '<span class="hljs-string">Access-Control-Max-Age</span>' => <span class="h...
<span class="hljs-type">$tx</span>-><span class="hljs-type">res</span>-><span class="hljs-type">headers</span>-><span class="hljs-type">header</span>( '<span class="hljs-string">Access-Control-Allow-Headers</span>' => '<sp...
};
any '<span class="hljs-string">/*path</span>' => <span class="hljs-keyword">sub </span>{
<span class="hljs-keyword">my</span> ( <span class="hljs-type">$c</span> ) = <span class="hljs-type">@_</span>;
<span class="hljs-comment"># Allow preflight OPTIONS request for XmlHttpRequest to succeed</span><span class="hljs-comment">
</span> <span class="hljs-keyword">return</span> <span class="hljs-type">$c</span>-><span class="hljs-type">rendered</span>( <span class="hljs-number">204</span> ) <span class="hljs-keyword">if</span> <span class="hljs-type">$c</span>-><span...
<span class="hljs-keyword">return</span> <span class="hljs-type">$c</span>-><span class="hljs-type">render</span>(
template => <span class="hljs-function">join</span>( '<span class="hljs-string">/</span>', <span class="hljs-function">uc</span> <span class="hljs-type">$c</span>-><span class="hljs-type">req</span>-><span class="hljs-type">m...
variant => <span class="hljs-type">$c</span>-><span class="hljs-type">app</span>-><span class="hljs-type">mode</span>,
( run in 0.354 second using v1.01-cache-2.11-cpan-4e96b696675 )