Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. A problem with network.curl and a possible solution
(Michael Litchard)
2. Re: A problem with network.curl and a possible solution
(Michael Litchard)
----------------------------------------------------------------------
Message: 1
Date: Fri, 22 Oct 2010 13:44:38 -0700
From: Michael Litchard <[email protected]>
Subject: [Haskell-beginners] A problem with network.curl and a
possible solution
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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])
------------------------------
Message: 2
Date: Fri, 22 Oct 2010 15:52:07 -0700
From: Michael Litchard <[email protected]>
Subject: [Haskell-beginners] Re: A problem with network.curl and a
possible solution
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
SOLVED
it was a problem with the example code, which I put here for other
hapless newbs.
change
CurlPostFields [ "login=" ++ user, "password=" ++ pass ] : method_POST
to
method_POST ++ [CurlPostFields [ "login=" ++ user, "password=" ++ pass ]]
Thanks to aristid from #freenode for the help.
I'm sticking with Network.Curl
On Fri, Oct 22, 2010 at 1:44 PM, Michael Litchard <[email protected]> wrote:
> 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])
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 35
*****************************************