Skip to content

Latest commit

 

History

History
57 lines (36 loc) · 1.36 KB

README.md

File metadata and controls

57 lines (36 loc) · 1.36 KB

HTTP in Elm

Make HTTP requests in Elm.

import Http
import Json.Decode as Decode


-- GET A STRING

getWarAndPeace : Http.Request String
getWarAndPeace =
  Http.getString "https://example.com/books/war-and-peace"


-- GET JSON

getMetadata : Http.Request Metadata
getMetadata =
  Http.get "https://example.com/books/war-and-peace/metadata" decodeMetadata

type alias Metadata =
  { author : String
  , pages : Int
  }

decodeMetadata : Decode.Decoder Metadata
decodeMetadata =
  Decode.map2 Metadata
    (Decode.field "author" Decode.string)
    (Decode.field "pages" Decode.int)


-- SEND REQUESTS

type Msg
  = LoadMetadata (Result Http.Error Metadata)

send : Cmd Msg
send =
  Http.send LoadMetadata getMetadata

Examples

Learn More

To understand how HTTP works in Elm, check out: