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.  Get responsecode(Int) from simpleHTTP's Response
      (Jacques du Rand)
   2. Re:  Get responsecode(Int) from simpleHTTP's      Response
      (Michael Orlitzky)
   3. Re:  Get responsecode(Int) from simpleHTTP's      Response (Tom Murphy)
   4. Re:  Get responsecode(Int) from simpleHTTP's      Response
      (Jacques du Rand)


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

Message: 1
Date: Tue, 16 Oct 2012 21:10:47 +0200
From: Jacques du Rand <[email protected]>
Subject: [Haskell-beginners] Get responsecode(Int) from simpleHTTP's
        Response
To: [email protected]
Message-ID:
        <CA+-VnXCVDkYBtoH9ff3KN3W2Z=kh4o1w_anxh2srouatlk-...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

HI all
I'm trying to write a function that gives me the HTTP code in Int

--This is broken
getStatusCode::Response->String
getStatusCode (Response _,x1,_,_) = x1

--this work the download  trying to get http status code as well
download_file fname url= do
let clean_uri = check_url url
putStrLn ("Downloading " ++ url ++ "...")
rsp <- simpleHTTP (defaultGETRequest_ clean_uri)
--problamatic function next line
                                                        print
(getStatusCode rsp)
file_buffer <- getResponseBody(rsp)
B.writeFile fname file_buffer
Best Regards

Jacques
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121016/f246ddf2/attachment-0001.htm>

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

Message: 2
Date: Tue, 16 Oct 2012 19:32:57 -0400
From: Michael Orlitzky <[email protected]>
Subject: Re: [Haskell-beginners] Get responsecode(Int) from
        simpleHTTP's    Response
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 10/16/2012 03:10 PM, Jacques du Rand wrote:
> HI all
> I'm trying to write a function that gives me the HTTP code in Int
> 
> --This is broken
> getStatusCode::Response->String
> getStatusCode (Response _,x1,_,_) = x1
> 
> --this work the download  trying to get http status code as well
> download_file fname url= do
> let clean_uri = check_url url
> putStrLn ("Downloading " ++ url ++ "...")
> rsp <- simpleHTTP (defaultGETRequest_ clean_uri) 
> --problamatic function next line
>                                                         print
> (getStatusCode rsp)
> file_buffer <- getResponseBody(rsp)
> B.writeFile fname file_buffer
> Best Regards


There are two reasons this isn't working...

The first is that simpleHTTP doesn't return a Response object. I'm
guessing from your variable name that you're expecting one. In fact, it
returns *either* an error *or* a Response object, so the first thing you
have to do before you deal with the response is check for an error.

The second problem is that the response code (within a Response object)
is not an integer -- it's an ordered pair of three integers (x,y,z). The
reason stated in the docs is so that it's easy to tell whether or not
you've got an OK/Error code on your hands.

This is the simplest thing I could come up with that does what you want.

  module Main
  where

  import Network.HTTP

  main :: IO ()
  main = do
    let req = getRequest "http://michael.orlitzky.com/";
    result <- simpleHTTP req
    case result of
      Left err -> do
        putStrLn "Error!"
      Right response -> do
        let (x,y,z) = rspCode response
        let hundreds = 100*x
        let tens = 10*y
        let ones = z
        let code = hundreds + tens + ones
        putStrLn $ "Response code: " ++ (show code)

    return ()


You could of course factor out the part that multiplies the (x,y,z) by
(100,10,1) into a function.



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

Message: 3
Date: Tue, 16 Oct 2012 20:06:35 -0400
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Get responsecode(Int) from
        simpleHTTP's    Response
To: Michael Orlitzky <[email protected]>
Cc: [email protected]
Message-ID:
        <cao9q0tvova8n4sepedyuiw8we+qew5czxrr64xt7y3_vsvn...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

You can replace all the multiplication with
concatMap show [x,y,z]
On Oct 16, 2012 7:34 PM, "Michael Orlitzky" <[email protected]> wrote:

> On 10/16/2012 03:10 PM, Jacques du Rand wrote:
> > HI all
> > I'm trying to write a function that gives me the HTTP code in Int
> >
> > --This is broken
> > getStatusCode::Response->String
> > getStatusCode (Response _,x1,_,_) = x1
> >
> > --this work the download  trying to get http status code as well
> > download_file fname url= do
> > let clean_uri = check_url url
> > putStrLn ("Downloading " ++ url ++ "...")
> > rsp <- simpleHTTP (defaultGETRequest_ clean_uri)
> > --problamatic function next line
> >                                                         print
> > (getStatusCode rsp)
> > file_buffer <- getResponseBody(rsp)
> > B.writeFile fname file_buffer
> > Best Regards
>
>
> There are two reasons this isn't working...
>
> The first is that simpleHTTP doesn't return a Response object. I'm
> guessing from your variable name that you're expecting one. In fact, it
> returns *either* an error *or* a Response object, so the first thing you
> have to do before you deal with the response is check for an error.
>
> The second problem is that the response code (within a Response object)
> is not an integer -- it's an ordered pair of three integers (x,y,z). The
> reason stated in the docs is so that it's easy to tell whether or not
> you've got an OK/Error code on your hands.
>
> This is the simplest thing I could come up with that does what you want.
>
>   module Main
>   where
>
>   import Network.HTTP
>
>   main :: IO ()
>   main = do
>     let req = getRequest "http://michael.orlitzky.com/";
>     result <- simpleHTTP req
>     case result of
>       Left err -> do
>         putStrLn "Error!"
>       Right response -> do
>         let (x,y,z) = rspCode response
>         let hundreds = 100*x
>         let tens = 10*y
>         let ones = z
>         let code = hundreds + tens + ones
>         putStrLn $ "Response code: " ++ (show code)
>
>     return ()
>
>
> You could of course factor out the part that multiplies the (x,y,z) by
> (100,10,1) into a function.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121016/e5624806/attachment-0001.htm>

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

Message: 4
Date: Wed, 17 Oct 2012 08:51:00 +0200
From: Jacques du Rand <[email protected]>
Subject: Re: [Haskell-beginners] Get responsecode(Int) from
        simpleHTTP's    Response
To: Michael Orlitzky <[email protected]>
Cc: [email protected]
Message-ID:
        <CA+-VnXDSLBHrdLZ+D4VAAZEhcJmRmSqK=ob_nap2nsrje_+...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

This is great works perfectly !
I'm so new at haskell its scary !

One last question i dont *understand* one line ( just the right side )

       let (x,y,z) = rspCode response

1) If i look at the documentation: Sorry for HTML. I see the constructors
of Response and below it rspCode,rspReason,rspHeaders etc Are
those parters or functions ?

2) I see the new version has a getResponseCode functions like
getResponseBody with the signature:
getResponseCode :: Result (Response a) -> IO ResponseCode
getResponseCode (Left err) = fail (show err)
getResponseCode (Right r)  = return (rspCode r)

What does this mean in the signature  *Result (Response a)*

Thanks again
Jacques


Constructors
Response
rspCode 
::ResponseCode<http://hackage.haskell.org/packages/archive/HTTP/4000.0.7/doc/html/Network-HTTP-Base.html#t:ResponseCode>
rspReason :: 
String<http://hackage.haskell.org/packages/archive/base/4.2.0.2/doc/html/Data-Char.html#t:String>
rspHeaders :: 
[Header<http://hackage.haskell.org/packages/archive/HTTP/4000.0.7/doc/html/Network-HTTP-Headers.html#t:Header>
]rspBody :: a


On Wed, Oct 17, 2012 at 1:32 AM, Michael Orlitzky <[email protected]>wrote:

> On 10/16/2012 03:10 PM, Jacques du Rand wrote:
> > HI all
> > I'm trying to write a function that gives me the HTTP code in Int
> >
> > --This is broken
> > getStatusCode::Response->String
> > getStatusCode (Response _,x1,_,_) = x1
> >
> > --this work the download  trying to get http status code as well
> > download_file fname url= do
> > let clean_uri = check_url url
> > putStrLn ("Downloading " ++ url ++ "...")
> > rsp <- simpleHTTP (defaultGETRequest_ clean_uri)
> > --problamatic function next line
> >                                                         print
> > (getStatusCode rsp)
> > file_buffer <- getResponseBody(rsp)
> > B.writeFile fname file_buffer
> > Best Regards
>
>
> There are two reasons this isn't working...
>
> The first is that simpleHTTP doesn't return a Response object. I'm
> guessing from your variable name that you're expecting one. In fact, it
> returns *either* an error *or* a Response object, so the first thing you
> have to do before you deal with the response is check for an error.
>
> The second problem is that the response code (within a Response object)
> is not an integer -- it's an ordered pair of three integers (x,y,z). The
> reason stated in the docs is so that it's easy to tell whether or not
> you've got an OK/Error code on your hands.
>
> This is the simplest thing I could come up with that does what you want.
>
>   module Main
>   where
>
>   import Network.HTTP
>
>   main :: IO ()
>   main = do
>     let req = getRequest "http://michael.orlitzky.com/";
>     result <- simpleHTTP req
>     case result of
>       Left err -> do
>         putStrLn "Error!"
>       Right response -> do
>         let (x,y,z) = rspCode response
>         let hundreds = 100*x
>         let tens = 10*y
>         let ones = z
>         let code = hundreds + tens + ones
>         putStrLn $ "Response code: " ++ (show code)
>
>     return ()
>
>
> You could of course factor out the part that multiplies the (x,y,z) by
> (100,10,1) into a function.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121017/6af629f4/attachment-0001.htm>

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

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


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

Reply via email to