Skip to content

Commit

Permalink
Add ExampleBuilder_BodySerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Sep 22, 2023
1 parent 6b07cc6 commit cf9ce0d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions builder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package requests_test
import (
"bytes"
"context"
"encoding/binary"
"encoding/csv"
"errors"
"fmt"
Expand Down Expand Up @@ -583,3 +584,36 @@ func ExampleBuilder_ErrorJSON() {
// X 1
// brewing
}

func ExampleBuilder_BodySerializer() {
// Have some binary data
data := struct {
Header [3]byte
Data uint32
}{
Header: [3]byte([]byte("ABC")),
Data: 0xbadc0fee,
}
// Serialize it by just shoving data onto the wire
serializer := func(v any) ([]byte, error) {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, v)
return buf.Bytes(), err
}
// Make a request using the serializer
req, err := requests.
New().
BodySerializer(serializer, data).
Request(context.Background())
if err != nil {
panic(err)
}
b, err := io.ReadAll(req.Body)
if err != nil {
panic(err)
}
// Request body is just serialized bytes
fmt.Printf("%q", b)
// Output:
// "ABC\xba\xdc\x0f\xee"
}

0 comments on commit cf9ce0d

Please sign in to comment.