Send a POST request to the given URL. Specify how to decode the response.
import Http
import Rails
createHat : Hat -> Cmd msg
createHat =
Rails.post
{ url = "https://example.com/hats"
, body = Http.jsonBody (encodeHat hat)
, expect = Rails.expectJson HandleNewHatResponse hatDecoder
}NOTE: 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 Rails
updateHat : Hat -> Cmd msg
updateHat hat =
Rails.put
{ url = "https://example.com/hats/" ++ String.fromInt hat.id
, body = Http.jsonBody (encodeHat hat)
, expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
}NOTE: 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.Encode exposing (object, string)
import Rails
updateHatDescription : Int -> String -> Cmd msg
updateHatDescription id description =
Rails.patch
{ url = "https://example.com/hats/" ++ String.fromInt hat.id
, body = Http.jsonBody (object [ ( "description", string description ) ])
, expect = Rails.expectJson HandleUpdatedHatResponse hatDecoder
}NOTE: 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 Rails
destroyHat : Hat -> Cmd msg
destroyHat =
Rails.delete
{ url = "https://example.com/hats/" ++ String.fromInt hat.id
, body = Http.emptyBody
, expect = Rails.expectEmptyBody HandleDeletedHatResponse
}NOTE: 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.
Wraps Http.request while adding the following default headers:
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.
The delete example above would look lik this:
import Http
import Rails
destroyHat : Hat -> Cmd msg
destroyHat =
Rails.request
{ method = "DELETE"
, headers = []
, url = "https://example.com/hats/" ++ String.fromInt hat.id
, body = Http.emptyBody
, expect = Rails.expectEmptyBody HandleDeletedHatResponse
, timeout = Nothing
, tracker = Nothing
}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.
Expect that the response body will look a certain way. Similar to
Http.Expect,
but wrapped here so we know what headers to set in requests.
The kinds of responses a Rails server may return.
Expect Rails to return some string data. If this will be JSON, use
expectJson instead!
Expect Rails to return an empty body. Note that we don't actually enforce
the body is empty; we just discard it. Pairs well with delete.
Expect Rails to return JSON.
Decode Rails-specific error information from a
BadStatus_
response (that is, a response whose status code is outside the 200 range.)
import Dict
import Http
import Json.Decode exposing (at, string)
import Json.Encode as Encode
import Rails
import Rails.Decode
createHat : Hat -> Cmd Msg
createHat hat =
let
errorsDecoder =
Rails.Decode.decodeErrors (at [ "errors", "style" ] string)
in
Rails.post
{ url = "https://example.com/hats"
, body = Http.jsonBody (encodeHat hat)
, expect = Rails.expectJsonErrors HandleNewHatResponse errorsDecoder hatDecoder
}
Send a GET request to the given URL. Specify how to decode the response.