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 =
Rails.decoder (list string) (succeed ())
|> Rails.get "http://example.com/hat-categories.json"
|> Rails.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 =
Rails.decoder (list string) (succeed ())
|> Rails.post "http://example.com/hat-categories/new" Http.emptyBody
|> Rails.send HandleResponseSend an HTTP request to Rails. This uses request under the
hood, which means that a CSRF token will automatically be passed, among
other things. The Error handler uses a Rails.Error, which includes
more information than the standard Http.Error.
import Dict
import Json.Decode exposing (list, string)
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
success =
list string
failure =
Dict.fromList [ ( "style", HatStyle ) ]
|> Rails.Decode.errors
in
Rails.decoder success failure
|> Rails.post url body
|> Rails.send HandleResponseWraps 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 GET
Content-Type - "application/json"
Accept - "application/json, text/javascript, */*; q=0.01"
X-Requested-With - "XMLHttpRequest"
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
success =
list string
failure =
Dict.fromList [ ( "style", HatStyle ) ]
|> Rails.Decode.errors
in
Rails.request
{ method = "POST"
, headers = []
, url = url
, body = body
, expect = expectRailsJson (Rails.decoder success failure)
, timeout = Nothing
, withCredentials = False
}Returns a ResponseDecoder.
Returns a ResponseDecoder which uses the same decoder for both success and
failures.
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).Think Http.fromJson, but with additional effort to parse a non-20x response as JSON.
* If the status code is in the 200 range, try to parse with the given decoder.success.
* If that succeeds, the result is Ok with the result.
* If the status code is outside the 200 range, try to parse with the given decoder.failure.
* If that succeeds, the result is Err with the result.
* If either parsing fails, the request as a whole fails.
The kinds of errors a Rails server may return.