Skip to content

Commit

Permalink
Add ExampleBuilder_HeaderOptional
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Jul 12, 2024
1 parent d385b84 commit e4d1efd
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions builder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,3 +726,26 @@ func ExampleBuilder_ParamOptional() {
// Output:
// https://www.example.com/?a=1&c=something
}

func ExampleBuilder_HeaderOptional() {
// Suppose we have some environment variables
// which may or may not be set
env := map[string]string{
"FOO": "1",
"BAR": "",
"BAZ": "",
}
req, err := requests.
URL("https://example.com").
HeaderOptional("X-FOO", env["FOO"]).
HeaderOptional("X-BAR", env["BAR"]). // Won't set because BAR is blank
Header("X-BAZ", env["BAZ"]). // Will set to "" because it's not optional
Request(context.Background())
if err != nil {
fmt.Println("Error!", err)
}
fmt.Println(req.Header)

// Output:
// map[X-Baz:[] X-Foo:[1]]
}

0 comments on commit e4d1efd

Please sign in to comment.