title: Using Network.URL library in Haskell
body: <p>After reading the Yesod book, I'm sold on the idea of using typesafe URLs rather than raw string-likes.  I'm just trying some simple get requests, rather than using the full yesod library, but seemed like it would be easy enough to either define a custom URL data type deriving show, or to use some other existing minimal URL library.</p>
  
  <p>I found this on Hoogle:
  <a href="https://hackage.haskell.org/package/url-2.1.3/docs/Network-URL.html" rel="nofollow noreferrer">https://hackage.haskell.org/package/url-2.1.3/docs/Network-URL.html</a></p>
  
  <p>Seems like it will do the kind of validation I want, so that a mistake like:</p>
  
  <pre><code>myothervar = "kittens"   -- oh no, I defined this for something else
  parseRequest myothervar  -- and called it from parseRequest!
  </code></pre>
  
  <p>will be caught by the compiler, while </p>
  
  <pre><code>myurl = "http://example.com"
  parseRequest myurl
  </code></pre>
  
  <p>...will succeed, great.</p>
  
  <pre><code>ghci    λ&gt; import Network.URL
  ghci    λ&gt; myurl = importURL "http://example.com"
  </code></pre>
  
  <p>Documentation suggests to me I need to use importURL </p>
  
  <pre><code>ghci    λ&gt; :t myurl
  myurl :: Maybe URL
  </code></pre>
  
  <p>Great, I know how to de-<code>Maybe</code>, I use <code>Just</code>.</p>
  
  <pre><code>ghci    λ&gt; exportURL $ Just myurl
  
  &lt;interactive&gt;:47:13: error:
      • Couldn't match expected type ‘URL’
                    with actual type ‘Maybe (Maybe URL)’
      • In the second argument of ‘($)’, namely ‘Just myurl’
        In the expression: exportURL $ Just myurl
        In an equation for ‘it’: it = exportURL $ Just myurl
  
  ghci    λ&gt; Just (Just (URL {url_type = Absolute (Host {protocol = HTTP False, host = "example.com", port = Nothing}), url_path = "", url_params = []}))
  </code></pre>
  
  <p>OK, what?  That didn't remove my Just, it just added another Just.  How can I retrieve the String that <code>parseRequest</code> will take as an argument?</p>
  
  <p>And it looks like I definitely need to de-Maybe it by exporting back to String somehow:</p>
  
  <pre><code>ghci    λ&gt; import Network.HTTP.Conduit
  ghci    λ&gt; parseRequest $ myurl
  
  &lt;interactive&gt;:84:16: error:
      • Couldn't match type ‘Maybe URL’ with ‘[Char]’
        Expected type: String
          Actual type: Maybe URL
  </code></pre>