Getting images from the creative commons api

At the end of
there is an exercise about showing images instead of numbers.

I guess this can be solved similar to what is done in:
but then not looking for cats, but for dices?

So i tried using“dice%20one”&page_size=1
(which isn’t really fullproof, as the dice shown might be the wrong one, but i thought it was a nice idea)

This is the code i changed in
to get the images, but this does not work unfortunately:

getRandomCatGif : Cmd Msg
getRandomCatGif =
    { url = ""
    , expect = Http.expectJson GotGif gifDecoder

gifDecoder : Decoder String
gifDecoder =
  field "results" (index 1 (field "url" string)) 

As the results element is an array
Any suggestions,
(or also am i on the wrong trail in for this exercise?)

field "results" (index 1 (field "url" string)) 

Almost but Json.Decode.index starts at 0, try with index 0.

Note that you also have to expose index or use it qualified:

import Json.Decode exposing (Decoder, field, string, index)


import Json.Decode as Decode
Decode.field "results" (Decode.index 1 (Decode.field "url" Decode.string)) 

Thank you,
so whenever there is something alike

I cannot find a index variable:

120| field “results” (index 0 (field “url” string))
These names seem close though:


Hint: Read to see how import
declarations work in Elm.

Using the qualified name could be one the solutions.

As the examples in don’t seems to use qualified names often,
is qualified or unqualified import preferred?
Looking around: suggests using qualified imports.

Qualified names are usually preferable, see Should examples on use qualified functions? for their use in examples and some references.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.