[Haskell-beginners] A problem with network.curl and a possible
solution
Michael Litchard
michael at schmong.org
Fri Oct 22 16:44:38 EDT 2010
I'm using the code from the following blog as a template to create a
session on a work server using ssl. To begin with, I am sticking with
the example site github.com. I have noticed that the code that does a
POST, does a GET request instead. I suspect this is a problem with the
underlying libcurl. I have decided to test out another library,
network.browser. If I get the same behavior, then I know I'm the one
screwing up. If I get the right behavior, I will move over to
network.browser. The problem is, I am having difficulty writing the
same functionality with network.browser. I don't know how to
"activate", or manage the cookie jar. And I don't know how to send
data with a POST request. The url, and sample code are below. I would
like a demonstration of how to accomplish the same this using
Network.Browser. With the help of the blog example, Network.Curl is
accessible enough , but I just can't get the POST request to work.
http://flygdynamikern.blogspot.com/2009/03/extended-sessions-with-haskell-curl.html
> import Network.Curl
> import System (getArgs)
> import Text.Regex.Posix
> -- | Standard options used for all requests. Uncomment the @CurlVerbose@
> -- option for lots of info on STDOUT.
I like the verbose output, and the way the cookie jar is automagically
made. I will manage the cookie jar if I have to, I just need to know
how.
> opts = [ CurlCookieJar "cookies" {- , CurlVerbose True -} ]
> -- | Additional options to simulate submitting the login form.
> loginOptions user pass =
> CurlPostFields [ "login=" ++ user, "password=" ++ pass ] : method_POST
> main = withCurlDo $ do
> -- Get username and password from command line arguments (will cause
> -- pattern match failure if incorrect number of args provided).
> [user, pass] <- getArgs
> -- Initialize curl instance.
> curl <- initialize
> setopts curl opts
> -- POST request to login.
> r <- do_curl_ curl "https://github.com/session" (loginOptions user pass)
> :: IO CurlResponse
> if respCurlCode r /= CurlOK || respStatus r /= 302
> then error $ "Failed to log in: "
> ++ show (respCurlCode r) ++ " -- " ++ respStatusLine r
> else do
> -- GET request to fetch account page.
> r <- do_curl_ curl ("https://github.com/account") method_GET
> :: IO CurlResponse
> if respCurlCode r /= CurlOK || respStatus r /= 200
> then error $ "Failed to retrieve account page: "
> ++ show (respCurlCode r) ++ " -- " ++ respStatusLine r
> else putStrLn $ extractToken $ respBody r
> -- | Extracts the token from GitHub account HTML page.
The code below is not important for my purposes,
> extractToken body = head' "GitHub token not found" xs
> where
> head' msg l = if null l then error msg else head l
> (_,_,_,xs) = body =~ "github\\.token (.+)"
> :: (String, String, String,[String])
More information about the Beginners
mailing list