<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>httpoison on Poeticoding</title><link>https://www.poeticoding.com/tag/httpoison/</link><description>Recent content in httpoison on Poeticoding</description><generator>Hugo</generator><language>en</language><lastBuildDate>Thu, 28 Nov 2019 15:56:38 +0000</lastBuildDate><atom:link href="https://www.poeticoding.com/tag/httpoison/index.xml" rel="self" type="application/rss+xml"/><item><title>Elixir Stream and large HTTP responses: processing text</title><link>https://www.poeticoding.com/elixir-stream-and-large-http-responses-processing-text/</link><pubDate>Wed, 03 Apr 2019 16:02:19 +0000</pubDate><guid>https://www.poeticoding.com/elixir-stream-and-large-http-responses-processing-text/</guid><description>&lt;ul>
&lt;li>Part 1 - &lt;a href="https://www.poeticoding.com/elixir-streams-to-process-large-http-responses-on-the-fly/">Elixir Stream to process large HTTP responses on the fly&lt;/a>&lt;/li>
&lt;li>Part 2 - &lt;em>this article&lt;/em>&lt;/li>
&lt;/ul>
&lt;p>You find the code, of this and the previous article, on &lt;a href="https://github.com/poeticoding/httpstream_articles" target="_blank" rel="noopener">poeticoding/httpstream_articles&lt;/a> GitHub repo. The code at this repo is not meant for production use, is just part of the examples of these articles.&lt;/p></description></item><item><title>Elixir Streams to process large HTTP responses on the fly</title><link>https://www.poeticoding.com/elixir-streams-to-process-large-http-responses-on-the-fly/</link><pubDate>Wed, 27 Mar 2019 16:48:21 +0000</pubDate><guid>https://www.poeticoding.com/elixir-streams-to-process-large-http-responses-on-the-fly/</guid><description>&lt;ul>
&lt;li>Part 1 - &lt;em>this article&lt;/em>&lt;/li>
&lt;li>Part 2 - &lt;a href="https://www.poeticoding.com/elixir-stream-and-large-http-responses-processing-text/">Elixir Stream and large HTTP responses: processing text&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>This is the first of a two-part article where we see a handy way of handling async HTTP responses.\&lt;/p>
&lt;ul>
&lt;li>In &lt;strong>this&lt;/strong> &lt;strong>first part&lt;/strong> we see how make an Elixir Stream out of a large HTTP response.&lt;/li>
&lt;li>In the &lt;strong>second part&lt;/strong> we will extend the implementation so we can download a large text file, processing lines on the fly.&lt;/li>
&lt;/ul>
&lt;h2 id="first-and-foremost--why">First and foremost &amp;hellip; WHY?&lt;/h2>
&lt;p>Why should we handle HTTP responses with Streams, when we can just simply use something like &lt;code>HTTPoison.get&lt;/code> to receive a response with the data we need?&lt;/p></description></item><item><title>The Primitives of Elixir Concurrency: a Full Example</title><link>https://www.poeticoding.com/the-primitives-of-elixir-concurrency-full-example/</link><pubDate>Tue, 19 Mar 2019 15:39:08 +0000</pubDate><guid>https://www.poeticoding.com/the-primitives-of-elixir-concurrency-full-example/</guid><description>&lt;p>Previous articles about Concurrency in Elixir:\&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://www.poeticoding.com/spawning-processes-in-elixir-a-gentle-introduction-to-concurrency/">How to spawn an Elixir process&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.poeticoding.com/hey-process-there-is-a-message-for-you/">How to send and receive messages between processes&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Let’s put in practice what we’ve seen in the last few articles about concurrency. In this article we see how to fully make our &lt;a href="https://www.poeticoding.com/spawning-processes-in-elixir-a-gentle-introduction-to-concurrency/#http_api_requests">initial cryptocurrency example&lt;/a>, using just &lt;a href="https://github.com/edgurgel/httpoison" target="_blank" rel="noopener">HTTPoison&lt;/a> module and &lt;code>spawn&lt;/code>, &lt;a href="https://hexdocs.pm/elixir/Kernel.html#send/2" target="_blank" rel="noopener">send&lt;/a> and &lt;code>receive&lt;/code> to handle concurrency. At the end we will refactor it using &lt;a href="https://hexdocs.pm/elixir/Task.html" target="_blank" rel="noopener">Task&lt;/a>, which makes everything far easier!&lt;/p></description></item><item><title>Download Large Files with HTTPoison Async Requests</title><link>https://www.poeticoding.com/download-large-files-with-httpoison-async-requests/</link><pubDate>Wed, 13 Feb 2019 16:37:55 +0000</pubDate><guid>https://www.poeticoding.com/download-large-files-with-httpoison-async-requests/</guid><description>&lt;p>With &lt;a href="https://github.com/edgurgel/httpoison" target="_blank" rel="noopener">HTTPoison&lt;/a> is really easy to do HTTP requests in Elixir. If we need to get the latest BTC-USD exchange rate from the Coinbase API, we just use the &lt;code>HTTPoison.get!&lt;/code> passing the url and query parameters.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-elixir" data-lang="elixir">&lt;span class="line">&lt;span class="cl">&lt;span class="n">iex&lt;/span>&lt;span class="o">&amp;gt;&lt;/span> &lt;span class="n">url&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;https://api.coinbase.com/v2/prices/spot&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">iex&lt;/span>&lt;span class="o">&amp;gt;&lt;/span> &lt;span class="n">resp&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nc">HTTPoison&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">get!&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">url&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">%{},&lt;/span> &lt;span class="ss">params&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="ss">currency&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;USD&amp;#34;&lt;/span>&lt;span class="p">])&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">%&lt;/span>&lt;span class="nc">HTTPoison.Response&lt;/span>&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="ss">body&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;{\&amp;#34;data\&amp;#34;:{\&amp;#34;amount\&amp;#34;:\&amp;#34;3585.005\&amp;#34; ... }}&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">...&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="ss">status_code&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="mi">200&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span> 
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">iex&lt;/span>&lt;span class="o">&amp;gt;&lt;/span> &lt;span class="p">%{&lt;/span>&lt;span class="s2">&amp;#34;data&amp;#34;&lt;/span> &lt;span class="o">=&amp;gt;&lt;/span> &lt;span class="p">%{&lt;/span>&lt;span class="s2">&amp;#34;amount&amp;#34;&lt;/span> &lt;span class="o">=&amp;gt;&lt;/span> &lt;span class="n">amount&lt;/span>&lt;span class="p">}}&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nc">Jason&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">decode!&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">resp&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">body&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">iex&lt;/span>&lt;span class="o">&amp;gt;&lt;/span> &lt;span class="nc">String&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">to_float&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">amount&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="mf">3585.005&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We easily get the response with a JSON body containing the information we need. We then use a library like &lt;a href="https://github.com/michalmuskala/jason" target="_blank" rel="noopener">Jason&lt;/a> to de-serialise the JSON string and then get the amount using pattern matching.&lt;/p></description></item></channel></rss>