我有以下代码,其中我进行了一个带有JWToken授权头的post请求。 我希望从响应头中提取JWToken,并使用端口将其保存在本地存储中。
如何获取Response? 我看到Response类型中的Metadata具有Headers。ref - https://package.elm-lang.org/packages/elm/http/latest/Http#Response
type Msg
= EnteredEmail String
| EnteredPassword String
| SubmittedForm
| RegistrationSuccess (Result Http.Error ())
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
EnteredEmail email ->
updateForm (\form -> { form | email = email }) model
EnteredPassword password ->
updateForm (\form -> { form | password = password }) model
RegistrationSuccess _->
-- TODO save JWT in local storage on successful registration
(model, Cmd.none)
SubmittedForm ->
-- TODO validate the form
(model, postCall model)
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 = Http.expectWhatever RegistrationSuccess
}