I'm trying to create an APP in Elm at the first time. And I need to interact with a server application using Http.post.
I have Msg type like this:
type Msg =
…
| Send (String, String)
| Recv (String, String)
and update function is like this:
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
…
Send (name, data) ->
(newModel, Http.post
{ url = url
, body = Http.multipartBody [Http.stringPart "data" data]
, expect = Http.expectString (Recv name)
})
Recv (name, data) -> … -- process data
But when I run this, it produces a type mismatch error in Http.post. So how can I pass name and newData to Msg "Recv" at the same time?
CodePudding user response:
Your type constructor Recv
is expecting a single argument which is a tuple of string. So essentially Recv : ( String, String ) -> Msg
.
So when you call Recv name
, where name : String
, you will get a type error.
There are two solutions here:
You change the definition of
Recv
to be| Recv String String
This will now mean that you can partially apply the
Recv
constructor and get back a functionString -> Msg
, which is whatHttp.expectString
wants.You change the expect call to get the right shape:
, expect = Http.expectString (\body -> Recv ( name, body ))
Here you explicitly make the function that
Http.expectString
wants.