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. Re:  Problem with Data.Map and IO... (Markus L?ll)
   2. Re:  Network.Curl POST body not showing up (Erik de Castro Lopo)
   3. Re:  Network.Curl POST body not showing up (Michael Xavier)
   4. Re:  Network.Curl POST body not showing up (Satoshi Nakamura)
   5. Re:  Network.Curl POST body not showing up (Erik de Castro Lopo)
   6. Re:  Problem with Data.Map and IO... (Manfred Lotz)


----------------------------------------------------------------------

Message: 1
Date: Thu, 12 May 2011 03:19:24 +0300
From: Markus L?ll <[email protected]>
Subject: Re: [Haskell-beginners] Problem with Data.Map and IO...
To: Michael Xavier <[email protected]>
Cc: [email protected], Manfred Lotz <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

You want the Map to map from filenames to their contents?

Basically you need mapM or foldM, depending in where you want to use
getXmlContent. In the below code I used mapM to read the files into a
list of pairs, and fold over it (I changed insertRec to take a pair).

--
module Main where

import Data.List
import qualified Data.Map as M
import Control.Monad (mapM)

insertRec :: M.Map String String -> (String, String) -> M.Map String String
insertRec m (key, value) =
   if M.lookup key m == Nothing
      then M.insert key value m
      else m

insertRec2 m (key, value) =
   M.insertWith const key value m


createMap :: [(String, String)] -> M.Map String String
createMap lst = foldl' insertRec M.empty lst


getXmlContent :: FilePath -> IO (String,String)
getXmlContent filename = do
   contents <- readFile filename
   return (filename, contents)

main :: IO ()
main = do
 xmlfiles <- mapM getXmlContent ["a.xml","f.xml", "a.xml"]
 let ht = createMap xmlfiles
 print ht
--

You could also read the files in insertRec instead of in main -- that
would make insertRec return an "IO (M.Map String String)" instead of
just "M.Map String String". And then you have to use foldM in
createMap to use it, because the folding function is monadic now.

On 5/11/11, Michael Xavier <[email protected]> wrote:
> I believe let when you're in do notation is for pure functions. Since c is
> IO (String String), I think what you might try is:
>
> c <- getXmlContent e
> k <- fst c
> v  <- snd c
>
> Or even better, use pattern matching:
>
> (k, v) <- getXmlContent e
>
> Someone please step in if I'm misrepresenting how it's done.
>
>
> On Wed, May 11, 2011 at 11:05 AM, Manfred Lotz <[email protected]>wrote:
>
>> Hi there,
>> I've got a problem creating a map.
>>
>> I have a list of dataset names and adding those dataset names into a
>> map works fine like this:
>>
>> <----------------------------snip-------------------------------->
>> module Main where
>>
>> import Data.List
>> import qualified Data.Map as M
>>
>>
>> insertRec :: M.Map String String -> String -> M.Map String String
>> insertRec m e =
>>  let f = M.lookup e m in
>>  if  f == Nothing
>>  then
>>     M.insert e  "bla" m
>>  else
>>     m
>>
>>
>> createMap :: [String] -> M.Map String String
>> createMap lst =
>>  let m = M.empty in
>>  foldl' insertRec m  lst
>>
>>
>> main :: IO ()
>> main = do
>>  let xmlfiles = [ "a.xml","f.xml"]
>>  let ht = createMap xmlfiles
>>  print ht
>> <----------------------------snap-------------------------------->
>>
>>
>> However, actually I want to parse the xml files and adding content into
>> the map.
>>
>> For this I have a function
>> getXmlContent :: FilePath -> IO (String,String)
>>
>> and would like to do something like this:
>>
>> <----------------------------snip-------------------------------->
>> insertRec m e = do
>>  c <- getXmlContent e
>>  let k = fst c
>>  let v = snd c
>>  M.insert  k v  m
>> <----------------------------snap-------------------------------->
>>
>> Unfortunately, this doesn't work because of IO. I have no glue how to
>> get this done.
>>
>>
>> Any help appreciated.
>>
>>
>>
>> --
>> Thanks,
>> Manfred
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
> Michael Xavier
> http://www.michaelxavier.net
>


-- 
Markus L?ll



------------------------------

Message: 2
Date: Thu, 12 May 2011 11:07:32 +1000
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Network.Curl POST body not showing up
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Bryce Verdier wrote:

> I would be interested in this as well. I'm trying to use Network.Curl 
> for a personal project, also using a method_post, and am not able to get 
> a response. Though I know it's connecting.
> 
> I _think_ that I need to use CurlWriteFunction, but I'm still new to 
> Haskell & not sure how to work with that option.

I tried a number of times to do things with Network.Curl and found
it to be a painful and frustrating experience. The API is highly
irregular and often remains too close to the underlying C API.

More recently I've been using the http-enumerator package which
is conceptually a little more complicated but works really,
really well. It does both HTTP and HTTPS, GET and POST. It
currently doesn't work via a proxy, but the http-enumerator
author has said he will take patches and I'm working on it.

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/



------------------------------

Message: 3
Date: Wed, 11 May 2011 19:48:43 -0700
From: Michael Xavier <[email protected]>
Subject: Re: [Haskell-beginners] Network.Curl POST body not showing up
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I've tried 3 different HTTP libraries for this project: Network.HTTP (which
doesn't support SSL), Network.Curl, and http-enumerator. This project
requires SSL, HTTP Basic Auth and Post. Network.HTTP does not support SSL
and http-enumerator does not support HTTP basic authentication. The
maintainer said he'd welcome a patch to it but I don't really have the time
or skill at this stage to implement something like that.

So I'm stuck with trying to coerce curl into doing what I need it to do.
Hopefully someone on this list has been able to successfully POST a string
body with Network.Curl

On Wed, May 11, 2011 at 6:07 PM, Erik de Castro Lopo
<[email protected]>wrote:

> Bryce Verdier wrote:
>
> > I would be interested in this as well. I'm trying to use Network.Curl
> > for a personal project, also using a method_post, and am not able to get
> > a response. Though I know it's connecting.
> >
> > I _think_ that I need to use CurlWriteFunction, but I'm still new to
> > Haskell & not sure how to work with that option.
>
> I tried a number of times to do things with Network.Curl and found
> it to be a painful and frustrating experience. The API is highly
> irregular and often remains too close to the underlying C API.
>
> More recently I've been using the http-enumerator package which
> is conceptually a little more complicated but works really,
> really well. It does both HTTP and HTTPS, GET and POST. It
> currently doesn't work via a proxy, but the http-enumerator
> author has said he will take patches and I'm working on it.
>
> Cheers,
> Erik
> --
> ----------------------------------------------------------------------
> Erik de Castro Lopo
> http://www.mega-nerd.com/
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Michael Xavier
http://www.michaelxavier.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110511/20634493/attachment-0001.htm>

------------------------------

Message: 4
Date: Thu, 12 May 2011 12:10:06 +0900
From: Satoshi Nakamura <[email protected]>
Subject: Re: [Haskell-beginners] Network.Curl POST body not showing up
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Micheal,

You cannot use curlGetString to POST since it always uses GET.

Here is a code snippet which I use. Although I don't use basic auth,
you can add another opt to use it.

{-# LANGUAGE ScopedTypeVariables #-}

import Control.Exception (IOException, handle)
import Control.Monad (liftM)
import qualified Data.ByteString as BSS
import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Lazy.Char8 as BS8
import Data.IORef
import qualified Network.Curl as Curl
import Network.URI (URI)


post :: URI -> BS.ByteString -> String -> IO (Maybe BS.ByteString)
post uri body contentType = handleIOException (const $ return Nothing)
$ Curl.withCurlDo $ do
        bodyRef <- newIORef []
        h <- Curl.initialize
        mapM_ (Curl.setopt h) $ [Curl.CurlURL $ show uri,
                                 Curl.CurlNoBody False,
                                 Curl.CurlFollowLocation False,
                                 Curl.CurlMaxRedirs 0,
                                 Curl.CurlAutoReferer False,
                                 Curl.CurlUserAgent "Mozilla/5.0",
                                 Curl.CurlNoSignal True,
--                                 Curl.CurlVerbose True,
                                 Curl.CurlPostFields [BS8.unpack body],
                                 Curl.CurlHttpHeaders ["Content-Type:
" ++ contentType],
                                 Curl.CurlWriteFunction $ bodyFunction bodyRef]
        code <- Curl.perform h
        if code /= Curl.CurlOK
            then return Nothing
            else liftM (Just . BS.fromChunks . reverse) $ readIORef bodyRef

bodyFunction :: IORef [BSS.ByteString] -> Curl.WriteFunction
bodyFunction r = Curl.gatherOutput_ $ \s -> do
                   bs <- BSS.packCStringLen s
                   modifyIORef r (bs:)

handleIOException :: (IOException -> IO a) -> IO a -> IO a
handleIOException handler action = handle (\(e :: IOException) ->
handler e) action

-- 
Satoshi Nakamura <[email protected]>


2011/5/12 Michael Xavier <[email protected]>:
> I've tried 3 different HTTP libraries for this project: Network.HTTP (which
> doesn't support SSL), Network.Curl, and http-enumerator. This project
> requires SSL, HTTP Basic Auth and Post. Network.HTTP does not support SSL
> and http-enumerator does not support HTTP basic authentication. The
> maintainer said he'd welcome a patch to it but I don't really have the time
> or skill at this stage to implement something like that.
> So I'm stuck with trying to coerce curl into doing what I need it to do.
> Hopefully someone on this list has been able to successfully POST a string
> body with Network.Curl
>
> On Wed, May 11, 2011 at 6:07 PM, Erik de Castro Lopo <[email protected]>
> wrote:
>>
>> Bryce Verdier wrote:
>>
>> > I would be interested in this as well. I'm trying to use Network.Curl
>> > for a personal project, also using a method_post, and am not able to get
>> > a response. Though I know it's connecting.
>> >
>> > I _think_ that I need to use CurlWriteFunction, but I'm still new to
>> > Haskell & not sure how to work with that option.
>>
>> I tried a number of times to do things with Network.Curl and found
>> it to be a painful and frustrating experience. The API is highly
>> irregular and often remains too close to the underlying C API.
>>
>> More recently I've been using the http-enumerator package which
>> is conceptually a little more complicated but works really,
>> really well. It does both HTTP and HTTPS, GET and POST. It
>> currently doesn't work via a proxy, but the http-enumerator
>> author has said he will take patches and I'm working on it.
>>
>> Cheers,
>> Erik
>> --
>> ----------------------------------------------------------------------
>> Erik de Castro Lopo
>> http://www.mega-nerd.com/
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> --
> Michael Xavier
> http://www.michaelxavier.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



------------------------------

Message: 5
Date: Thu, 12 May 2011 14:09:55 +1000
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Network.Curl POST body not showing up
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Michael Xavier wrote:

> I've tried 3 different HTTP libraries for this project: Network.HTTP (which
> doesn't support SSL), Network.Curl, and http-enumerator. This project
> requires SSL, HTTP Basic Auth and Post. Network.HTTP does not support SSL
> and http-enumerator does not support HTTP basic authentication. The
> maintainer said he'd welcome a patch to it but I don't really have the time
> or skill at this stage to implement something like that.

Nakamura-san has provided you with some code that gets you part way
to where you need to be.

Basic auth is something I'm going to need in http-enumerator pretty 
soon so I'll probably have a stab at it over the next week or two.
If you're interested in that and you haven't heard back from me
in say two weeks, feel free to ping me.

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/



------------------------------

Message: 6
Date: Thu, 12 May 2011 11:08:59 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] Problem with Data.Map and IO...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hi,
Thanks to both of you.


On Thu, 12 May 2011 03:19:24 +0300
Markus L?ll <[email protected]> wrote:

> You want the Map to map from filenames to their contents?
> 
> Basically you need mapM or foldM, depending in where you want to use
> getXmlContent. In the below code I used mapM to read the files into a
> list of pairs, and fold over it (I changed insertRec to take a pair).
> 

Actually I had tried foldM because I thought this has to be the way.
But when I changed createMap accordingly I didn't think about changing
the signature and then I didn't understand the compiler's error
message. 

foldM works fine now. Now I have another problem which should be a new
thread.



-- 
Manfred





------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 22
*****************************************

Reply via email to