@@ -203,13 +203,6 @@ type Header = Header String String
203
203
header "If-Modified-Since" "Sat 29 Oct 1994 19:43:31 GMT"
204
204
header "Max-Forwards" "10"
205
205
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
213
206
-}
214
207
header : String -> String -> Header
215
208
header =
@@ -403,22 +396,58 @@ bytesPart key mime bytes =
403
396
type Expect msg = Expect
404
397
405
398
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`.
407
416
-}
408
417
expectString : (Result Error String -> msg ) -> Expect msg
409
418
expectString toMsg =
410
419
expectStringResponse toMsg ( resolve Ok )
411
420
412
421
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)
414
441
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.
417
445
418
446
[here]: https://guide.elm-lang.org/interop/json.html
419
447
[json]: /packages/elm/json/latest/
420
448
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.
422
451
-}
423
452
expectJson : (Result Error a -> msg ) -> Decode .Decoder a -> Expect msg
424
453
expectJson toMsg decoder =
@@ -427,12 +456,30 @@ expectJson toMsg decoder =
427
456
Result . mapError Decode . errorToString ( Decode . decodeString decoder string)
428
457
429
458
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
431
464
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)
434
467
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.
436
483
-}
437
484
expectBytes : (Result Error a -> msg ) -> Bytes .Decoder a -> Expect msg
438
485
expectBytes toMsg decoder =
@@ -442,6 +489,22 @@ expectBytes toMsg decoder =
442
489
443
490
444
491
{- | 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.
445
508
-}
446
509
expectWhatever : (Result Error () -> msg ) -> Expect msg
447
510
expectWhatever toMsg =
0 commit comments