您可以使用Http.expectStringResponse或Http.expectBytesResponse而不是Http.expectWhatever来获得Response和标头.
这是一个示例,它定义了一个便捷函数ExpectJWT,它将检索并返回Authorization标头,如果不存在,则返回BadStatus403.在postCall中,所有更改都在于Http.expectWhatever已被ExpectJWT取代:
expectJWT : (Result Http.Error String -> msg) -> Http.Expect msg
expectJWT toMsg =
Http.expectStringResponse toMsg
esponse ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.Timeout_ ->
Err Http.Timeout
Http.NetworkError_ ->
Err Http.NetworkError
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
Http.GoodStatus_ metadata body ->
metadata.headers
|> Dict.get "Authorization"
|> Result.fromMaybe (Http.BadStatus 403)
postCall : Model -> Cmd Msg
postCall model = Http.post {
url = "http://localhost:9000/register",
body = Http.jsonBody (
Json.Encode.object[
("age", Json.Encode.int 30),
("email", Json.Encode.string model.form.email),
("password", Json.Encode.string model.form.password)
]
),
expect = expectJWT RegistrationSuccess
}