Send a GET request to the given URL. Specify how to decode the response.
import Http
import Json.Decode exposing (list, string, succeed)
import Rails
getHats : Cmd msg
getHats =
list hatDecoder
|> Rails.get "http://example.com/hat-categories.json"
|> Http.send HandleGetHatsResponseSend a POST request to the given URL. Specify how to decode the response.
import Http
import Json.Decode exposing (list, string, succeed)
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.post "http://example.com/hat-categories/new" Http.emptyBody
|> Http.send HandleResponseNOTE: Rails typically expects an X-CSRF-Token header for POST
requests, which this does not include. To have this header included
automatically, add csrf-xhr
to the page, before your Elm program gets initialized.
Send a PUT request to the given URL. Specify how to decode the response.
import Http
import Json.Decode exposing (list, string, succeed)
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.put "http://example.com/hat-categories/5" revisedHatData
|> Http.send HandleResponseNOTE: Rails typically expects an X-CSRF-Token header for PUT
requests, which this does not include. To have this header included
automatically, add csrf-xhr
to the page, before your Elm program gets initialized.
Send a PATCH request to the given URL. Specify how to decode the response.
import Http
import Json.Decode exposing (list, string, succeed)
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.patch "http://example.com/hat-categories/5" revisedHatData
|> Http.send HandleResponseNOTE: Rails typically expects an X-CSRF-Token header for PATCH
requests, which this does not include. To have this header included
automatically, add csrf-xhr
to the page, before your Elm program gets initialized.
Send a DELETE request to the given URL. Specify how to decode the response.
import Http
import Json.Decode exposing (list, string, succeed)
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.delete "http://example.com/hat-categories/5" Http.emptyBody
|> Http.send HandleResponseNOTE: Rails typically expects an X-CSRF-Token header for DELETE
requests, which this does not include. To have this header included
automatically, add csrf-xhr
to the page, before your Elm program gets initialized.
Decode Rails-specific error information from a BadStatus
response. (That is, a response whose status code is outside the 200 range.)
This is intended to be used with Http.send
like so:
import Dict
import Http
import Json.Decode exposing (at, list, string)
import Json.Encode as Encode
import Rails
import Rails.Decode
requestHats : HatStyle -> Cmd Msg
requestHats style =
let
body =
[ ( "style", encodeHatStyle style ) ]
|> Encode.object
|> Http.jsonBody
getErrors =
at [ "errors", "style" ] string
|> Rails.decodeErrors
in
list string
|> Rails.post url body
|> Http.send (getErrors >> HandleResponse)Decode Rails-specific error information from a BadStatus
response. (That is, a response whose status code is outside the 200 range.)
See also, decodeErrors.
Wraps Http.request while adding the following default headers:
Content-Type - "application/json"Accept - "application/json, text/javascript, */*; q=0.01"X-Requested-With - "XMLHttpRequest"You can specify additional headers in the headers field of the configuration record.
import Dict
import Http
import Json.Decode exposing (list, string)
import Json.Encode as Encode
import Rails
import Rails.Decode
hatRequest : HatStyle -> Request (Result (ErrorList Field) Hat)
hatRequest style =
let
body =
[ ( "style", encodeHatStyle style ) ]
|> Encode.object
|> Http.jsonBody
in
Rails.request
{ method = "POST"
, headers = []
, url = url
, body = body
, expect = Http.expectJson (list string)
, timeout = Nothing
, withCredentials = False
}NOTE: Rails typically expects an X-CSRF-Token header for requests other
than GET, which this does not include. One way to have this header included
automatically is to add csrf-xhr
to the page, before your Elm program gets initialized.
The kinds of errors a Rails server may return.