Skip to content

Commit 34a9a27

Browse files
committed
Improve docs a bit
Thanks @BrianHicks for the recommendations here!
1 parent 8f698e2 commit 34a9a27

File tree

1 file changed

+79
-16
lines changed

1 file changed

+79
-16
lines changed

src/Http.elm

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,6 @@ type Header = Header String String
203203
header "If-Modified-Since" "Sat 29 Oct 1994 19:43:31 GMT"
204204
header "Max-Forwards" "10"
205205
header "X-Requested-With" "XMLHttpRequest"
206-
207-
**Note:** In the future, we may split this out into an `Http.Headers` module
208-
and provide helpers for cases that are common on the client-side. If this
209-
sounds nice to you, open an issue [here][] describing the helper you want and
210-
why you need it.
211-
212-
[here]: https://github.com/elm/http/issues
213206
-}
214207
header : String -> String -> Header
215208
header =
@@ -403,22 +396,58 @@ bytesPart key mime bytes =
403396
type Expect msg = Expect
404397

405398

406-
{-| Expect the response body to be a `String`.
399+
{-| Expect the response body to be a `String`. Like when getting the full text
400+
of a book:
401+
402+
import Http
403+
404+
type Msg
405+
= GotText (Result Http.Error String)
406+
407+
getPublicOpinion : Cmd Msg
408+
getPublicOpinion =
409+
Http.get
410+
{ url = "https://elm-lang.org/assets/public-opinion.txt"
411+
, expect = Http.expectString GotText
412+
}
413+
414+
The response body is always some sequence of bytes, but in this case, we
415+
expect it to be UTF-8 encoded text that can be turned into a `String`.
407416
-}
408417
expectString : (Result Error String -> msg) -> Expect msg
409418
expectString toMsg =
410419
expectStringResponse toMsg (resolve Ok)
411420

412421

413-
{-| Expect the response body to be JSON.
422+
{-| Expect the response body to be JSON. Like if you want to get a random cat
423+
GIF you might say:
424+
425+
import Http
426+
import Json.Decode exposing (Decoder, field, string)
427+
428+
type Msg
429+
= GotGif (Result Http.Error String)
430+
431+
getRandomCatGif : Cmd Msg
432+
getRandomCatGif =
433+
Http.get
434+
{ url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cat"
435+
, expect = Http.expectJson GotGif gifDecoder
436+
}
437+
438+
gifDecoder : Decoder String
439+
gifDecoder =
440+
field "data" (field "image_url" string)
414441
415-
The official guide has a section on JSON [here][]. It will teach you how to
416-
use [`elm/json`][json] to turn a JSON body into an Elm value.
442+
The official guide goes through this particular example [here][]. That page
443+
also introduces [`elm/json`][json] to help you get started turning JSON into
444+
Elm values in other situations.
417445
418446
[here]: https://guide.elm-lang.org/interop/json.html
419447
[json]: /packages/elm/json/latest/
420448
421-
If the JSON decoder fails, you get a `BadBody` error.
449+
If the JSON decoder fails, you get a `BadBody` error that tries to explain
450+
what went wrong.
422451
-}
423452
expectJson : (Result Error a -> msg) -> Decode.Decoder a -> Expect msg
424453
expectJson toMsg decoder =
@@ -427,12 +456,30 @@ expectJson toMsg decoder =
427456
Result.mapError Decode.errorToString (Decode.decodeString decoder string)
428457

429458

430-
{-| Expect the response body to be binary data.
459+
{-| Expect the response body to be binary data. For example, maybe you are
460+
talking to an endpoint that gives back ProtoBuf data:
461+
462+
import Bytes.Decode as Bytes
463+
import Http
431464
432-
Use [`elm/bytes`](/packages/elm/bytes/latest/) to decode the binary data into
433-
Elm values.
465+
type Msg
466+
= GotData (Result Http.Error Data)
434467
435-
If the decoder fails, you get a `BadBody` error.
468+
getData : Cmd Msg
469+
getData =
470+
Http.get
471+
{ url = "/data"
472+
, expect = Http.expectBytes GotData dataDecoder
473+
}
474+
475+
-- dataDecoder : Bytes.Decoder Data
476+
477+
You would use [`elm/bytes`](/packages/elm/bytes/latest/) to decode the binary
478+
data according to a proto definition file like `example.proto`.
479+
480+
If the decoder fails, you get a `BadBody` error that just indicates that
481+
_something_ went wrong. It probably makes sense to debug by peeking at the
482+
bytes you are getting in the browser developer tools or something.
436483
-}
437484
expectBytes : (Result Error a -> msg) -> Bytes.Decoder a -> Expect msg
438485
expectBytes toMsg decoder =
@@ -442,6 +489,22 @@ expectBytes toMsg decoder =
442489

443490

444491
{-| Expect the response body to be whatever. It does not matter. Ignore it!
492+
For example, you might want this when uploading files:
493+
494+
import Http
495+
496+
type Msg
497+
= Uploaded (Result Http.Error ())
498+
499+
upload : File -> Cmd Msg
500+
upload file =
501+
Http.post
502+
{ url = "/upload"
503+
, body = Http.fileBody file
504+
, expect = Http.expectWhatever Uploaded
505+
}
506+
507+
The server may be giving back a response body, but we do not care about it.
445508
-}
446509
expectWhatever : (Result Error () -> msg) -> Expect msg
447510
expectWhatever toMsg =

0 commit comments

Comments
 (0)