Reputation: 997
I want to create a function that will make a request to the server and returns decoded value. I have custom headers within my request so I have to use Http.send function.
So far I was able to create task
getCurrentUser userId authToken err ok =
let
request =
Http.send defaultSettings
{ verb = "GET"
, headers = [("X-Auth", authToken)]
, url = "http://os.apiary.com"
, body = empty
}
in
Task.perform err ok request
type alias User = { name : String, age : Maybe Int }
userDecoder = object2 User ("name" := string) (maybe ("age" := int))
but I don't know where to put decode logic.
Upvotes: 1
Views: 329
Reputation: 36385
I think you're looking for fromJson
:
getCurrentUser userId authToken err ok =
let
request =
Http.send defaultSettings
{ verb = "GET"
, headers = [("X-Auth", authToken)]
, url = "http://os.apiary.com"
, body = empty
}
in
Http.fromJson userDecoder request
|> Task.perform err ok
Upvotes: 3