You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importHttpimportHttpBuilderimportJson.DecodeasDecodeimportJson.EncodeasEncodeimportUrl.BuilderasUrlBuildertype Status a =Loading|Loaded a |Failuretype alias Model={ items :Status(ListString)}itemsDecoder:Decode.Decoder (ListString)
itemsDecoder =Decode.list Decode.string
itemEncoder:String->Encode.ValueitemEncoder item =Encode.object
[("item",Encode.string item)]{-| addItem will send a post request to`"https://example.com/api/items?hello=world"` with the given JSON body and acustom header. It'll try to decode with `itemsDecoder`.-}addItem:String->CmdMsgaddItem item =UrlBuilder.crossOrigin
"https://example.com"["api","items"][UrlBuilder.string "hello""world"]|>HttpBuilder.post
|>HttpBuilder.withHeader "X-My-Header""Some Header Value"|>HttpBuilder.withJsonBody (itemEncoder item)|>HttpBuilder.withTimeout 10000|>HttpBuilder.withExpect (Http.expectJson GotItem itemsDecoder)|>HttpBuilder.request
type Msg=GotItem(ResultHttp.Error(ListString))update:Msg->Model-> ( Model, CmdMsg )
update msg model =case msg ofGotItem(Ok items)->({ model | items =Loaded items },Cmd.none
)GotItem(Err err)->({ model | items =Failure},Cmd.none)