Search code examples
frpelm

Two sequential HTTP requests


Sorry for newbie's question (and for my english) :)

I tries to write the following function:

  • the function downloads a content from URL1 (it's received as argument)
  • the function parses this content and extract URL2
  • the function downloads a content from URL2
  • the content from URL2 is a result of this function
  • if an error was occured, this function should return Nothing

I know how to execute the HTTP requests. I have a function to parse the request from URL1. But I don't know how:

  • to execute new request with extracted URL2
  • to ignore second request if URL2 isn't extracted (or error in URL1 is occured)

Solution

  • I principle you want something like this:

    import Maybe
    import Http
    
    type Url = String
    getContentFromUrl : Maybe Url -> Maybe String
    getContentFromUrl url = --your implementation
    extractUrlFromContent : Maybe String -> Maybe Url
    extractUrlFromContent content = --your implementation
    content = getContentFromUrl (Just "http://example.com") 
              |> extractUrlFromContent 
              |> getContentFromUrl