Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Node/HTTP/Client.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ module Node.HTTP.Client
, setTimeout
, httpVersion
, responseHeaders
, responseCookies
, statusCode
, statusMessage
) where

import Prelude (Unit, (<<<))
import Prelude (Unit, (<<<), ($))

import Control.Monad.Eff (Eff)
import Data.Maybe (Maybe)
import Data.Foreign (Foreign, toForeign)
import Data.Options (Options, Option, options, opt)
import Data.StrMap (StrMap())
import Data.StrMap (StrMap(), delete, lookup)
import Node.HTTP (HTTP())
import Node.Stream (Readable, Writable)
import Node.URL as URL
Expand Down Expand Up @@ -99,9 +101,17 @@ foreign import setTimeout :: forall eff. Request -> Int -> Eff (http :: HTTP | e
httpVersion :: Response -> String
httpVersion = _.httpVersion <<< unsafeCoerce

headers' :: forall a. Response -> StrMap a
headers' = _.headers <<< unsafeCoerce

-- | Get the response headers as a hash
-- | Cookies are not included and could be retrieved with responseCookies
responseHeaders :: Response -> StrMap String
responseHeaders = _.headers <<< unsafeCoerce
responseHeaders res = delete "set-cookie" $ headers' res

-- | Get the response cookies as Just (Array String) or Nothing if no cookies
responseCookies :: Response -> Maybe (Array String)
responseCookies res = lookup "set-cookie" $ headers' res

-- | Get the response status code
statusCode :: Response -> Int
Expand Down
12 changes: 11 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Node.HTTP (HTTP, listen, createServer, setHeader, requestMethod, requestU
import Node.Stream (Writable, end, pipe, writeString)
import Node.HTTP.Client as Client
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
import Data.Foldable (foldMap)
import Partial.Unsafe (unsafeCrashWith)

Expand All @@ -17,6 +17,7 @@ main :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
main = do
testBasic
testHttps
testCookies

testBasic :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
testBasic = do
Expand Down Expand Up @@ -48,10 +49,19 @@ testHttps :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
testHttps =
simpleReq "https://pursuit.purescript.org/packages/purescript-node-http/badge"

testCookies :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
testCookies =
simpleReq
"https://httpbin.org/cookies/set?cookie1=firstcookie&cookie2=secondcookie"

simpleReq :: forall eff. String -> Eff (console :: CONSOLE, http :: HTTP | eff) Unit
simpleReq uri = do
log ("GET " <> uri <> ":")
req <- Client.requestFromURI uri \response -> void do
log "Headers:"
logShow $ Client.responseHeaders response
log "Cookies:"
logShow $ Client.responseCookies response
log "Response:"
let responseStream = Client.responseAsStream response
pipe responseStream stdout
Expand Down