marocchino / http

Make HTTP requests in Elm (with rate limiting or progress tracking)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

About

Make HTTP requests in Elm (with rate limiting or progress tracking)

License:Other


Languages

Language:Elm 77.3%Language:JavaScript 22.7%