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 &#39;p.title &gt; a.title&#39; 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&#39;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&#39;t even need a browser!</p>

devdata/https_mojolicious.io_blog_2017_12_08_day-8-mocking-a-rest-api  view on Meta::CPAN

    { &quot;ip&quot;: &quot;10.0.0.2&quot;, &quot;os&quot;: &quot;Debian 8&quot; }
]
</code></pre>

<p>And also one of the individual item endpoints:</p>

<pre><code>$ perl test-api.pl get /servers/1
{ &quot;ip&quot;: &quot;10.0.0.1&quot;, &quot;os&quot;: &quot;Debian 9&quot; }
</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&#39;t work like that. So, I need to be able to
provide different data for different request methods. To do that, let&#39;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 &#39;<span class="hljs-string">/*path</span>&#39; =&gt; <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>-&gt;<span class="hljs-type">render</span>(
        template =&gt; <span class="hljs-function">join</span>( &#39;<span class="hljs-string">/</span>&#39;, <span class="hljs-function">uc</span> <span class="hljs-type">$c</span>-&gt;<span class="hljs-type">req</span>-&gt;<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&#39;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 =&gt; <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>-&gt;<span class="hljs-type">res</span>-&gt;<span class="hljs-type">headers</span>-&gt;<span class="hljs-type">header</span>( &#39;<span class="hljs-string">Access-Control-Allow-Origin</span>&#39; =&gt; &#39;<spa...
    <span class="hljs-type">$tx</span>-&gt;<span class="hljs-type">res</span>-&gt;<span class="hljs-type">headers</span>-&gt;<span class="hljs-type">header</span>( &#39;<span class="hljs-string">Access-Control-Allow-Methods</span>&#39; =&gt; &#39;<sp...
    <span class="hljs-type">$tx</span>-&gt;<span class="hljs-type">res</span>-&gt;<span class="hljs-type">headers</span>-&gt;<span class="hljs-type">header</span>( &#39;<span class="hljs-string">Access-Control-Max-Age</span>&#39; =&gt; <span class="h...
    <span class="hljs-type">$tx</span>-&gt;<span class="hljs-type">res</span>-&gt;<span class="hljs-type">headers</span>-&gt;<span class="hljs-type">header</span>( &#39;<span class="hljs-string">Access-Control-Allow-Headers</span>&#39; =&gt; &#39;<sp...
};
any &#39;<span class="hljs-string">/*path</span>&#39; =&gt; <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>-&gt;<span class="hljs-type">rendered</span>( <span class="hljs-number">204</span> ) <span class="hljs-keyword">if</span> <span class="hljs-type">$c</span>-&gt;<span...
    <span class="hljs-keyword">return</span> <span class="hljs-type">$c</span>-&gt;<span class="hljs-type">render</span>(
        template =&gt; <span class="hljs-function">join</span>( &#39;<span class="hljs-string">/</span>&#39;, <span class="hljs-function">uc</span> <span class="hljs-type">$c</span>-&gt;<span class="hljs-type">req</span>-&gt;<span class="hljs-type">m...
        variant =&gt; <span class="hljs-type">$c</span>-&gt;<span class="hljs-type">app</span>-&gt;<span class="hljs-type">mode</span>,



( run in 0.354 second using v1.01-cache-2.11-cpan-4e96b696675 )