Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm get raw JSON string

Tags:

elm

I'm trying to build a very simple app that will just output the raw JSON object from an api.

So I want a function that will take a url parameter and ideally return the JSON string.

I have the following code:

decode: String -> String
decode jsonString =
  Decode.decodeString jsonString

apiResonse : String -> String
apiResonse url =
  let
    url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cats"
    request = Http.get Decode.decodeString url
  in
    Http.send NewRequest request

But I'm struggling to understand the decoder part of the function. If anyone could help me that would be great.

like image 564
James Avatar asked Jan 25 '26 22:01

James


1 Answers

If you just want to get the HTTP response as a string value, use Http.getString. The example you posted using Http.get assumes the result is in JSON and forces you to decode it to an Elm value.

Here is a modified example of the random cat generator code which just displays a dump of the response JSON instead of a cat picture:

getRandomGif : String -> Cmd Msg
getRandomGif topic =
  let
    url =
      "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=" ++ topic
  in
    Http.send NewGif (Http.getString url)

Here is a working example on ellie-app.com

like image 107
Chad Gilbert Avatar answered Jan 29 '26 03:01

Chad Gilbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!