I have this small program that makes an HTTP request and prints the result:
module HttpClient where
import Prelude
import Data.Either (Either(..))
import Data.HTTP.Method (Method(..))
import Effect (Effect)
import Effect.Aff (Fiber, launchAff)
import Effect.Class (liftEffect)
import Effect.Console (log)
import Network.HTTP.Affjax as AX
import Network.HTTP.Affjax.Response as AXRes
main :: Effect (Fiber Unit)
main = launchAff $ do
let req = AX.defaultRequest { url = "http://ipecho.net/plain",
method = Left GET }
res <- AX.affjax AXRes.string req
liftEffect $ log $ "Your IP address: " <> res.response
When I run this program using pulp run -m HttpClient
, I sometimes get this error:
/Users/fhsu/work/purescript-examples/output/Effect.Aff/foreign.js:513
throw util.fromLeft(step);
^
Error: AJAX request failed: GET http://ipecho.net/plain
at XMLHttpRequest.xhr.onerror (/Users/fhsu/work/purescript-examples/output/Network.HTTP.Affjax/foreign.js:57:17)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/fhsu/work/purescript-examples/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._dispatchProgress (/Users/fhsu/work/purescript-examples/node_modules/xhr2/lib/xhr2.js:555:12)
at XMLHttpRequest._onHttpRequestError (/Users/fhsu/work/purescript-examples/node_modules/xhr2/lib/xhr2.js:545:12)
at ClientRequest.<anonymous> (/Users/fhsu/work/purescript-examples/node_modules/xhr2/lib/xhr2.js:414:24)
at ClientRequest.emit (events.js:182:13)
at TLSSocket.socketErrorListener (_http_client.js:382:9)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
The weird thing is that it tends to fail on the first execution, but usually succeeds on successive attempts.