Send a GET request to the given URL. Specify how to decode the response.
import Json.Decode exposing (list, string, succeed)
import Http
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 Json.Decode exposing (list, string, succeed)
import Http
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.post "http://example.com/hat-categories/new" Http.emptyBody
|> Http.send HandleResponseSend a PUT request to the given URL. Specify how to decode the response.
import Json.Decode exposing (list, string, succeed)
import Http
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.put "http://example.com/hat-categories/5" revisedHatData
|> Http.send HandleResponseSend a DELETE request to the given URL. Specify how to decode the response.
import Json.Decode exposing (list, string, succeed)
import Http
import Rails
hats : Cmd msg
hats =
list hatDecoder
|> Rails.delete "http://example.com/hat-categories/5" Http.emptyBody
|> Http.send HandleResponseDecode 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 Json.Decode exposing (list, string, at)
import Json.Encode as Encode
import Http
import Rails.Decode
import Rails
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:
X-CSRF-Token - set to csrfToken if it's an Ok and this request isn't a GETContent-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 Json.Decode exposing (list, string)
import Json.Encode as Encode
import Http
import Rails.Decode
import Rails
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
}If there was a <meta name="csrf-token"> tag in the page's <head> when
elm-rails loaded, returns the value its content attribute had at that time.
Rails expects this value in the `X-CSRF-Token` header for non-`GET` requests as
a [CSRF countermeasure](http://guides.rubyonrails.org/security.html#csrf-countermeasures).
The kinds of errors a Rails server may return.