marcosh
marcosh

Reputation: 9008

Elm - retrieving string via get request

I am trying to make a get request to retrieve a string

When I use

retrieve : Task.Task Http.Error String
retrieve = getString "http://api.endpoint.com"

everything works fine.

On the other hand if I use

retrieve : Task.Task Http.Error String
retrieve = get Json.Decode.string "http://api.endpoint.com"

the http request gets done, but the chained tasks are not executed.

My question is: what is the difference between the two approaches above? Am I doing something wrong with the second one? How to debug it?

Upvotes: 2

Views: 530

Answers (1)

Apanatshka
Apanatshka

Reputation: 5958

getString returns the response of the get request as a String. get take a JSON decoder and runs that over the response of the get request. So if you provide Json.Decode.string, it will expect the response to be have a Json encoded string in it. So it expects extra double quotes in the response.

If your http request fails the best way to debug is to look at what kind of error you get. In this case you'll probably get an UnexpectedPayload because the request succeeds, but the decoder fails.

Upvotes: 4

Related Questions