-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#23): persist context data across body updates
- Loading branch information
Showing
5 changed files
with
106 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/h2non/gentleman.v1" | ||
) | ||
|
||
func main() { | ||
// Create new request instance | ||
req := gentleman.NewRequest() | ||
req.Method("GET") | ||
|
||
// Define target URL | ||
req.URL("http://httpbin.org/headers") | ||
|
||
// Set a new header field | ||
req.SetHeader("Client", "gentleman") | ||
|
||
// Set sample context data | ||
req.Context.Set("foo", "bar") | ||
req.Context.Set("bar", "baz") | ||
|
||
// Set sample body as string | ||
req.BodyString("hello, gentleman!") | ||
|
||
// Output all context data | ||
fmt.Println(req.Context.GetAll()) | ||
|
||
// Perform the request | ||
res, err := req.Do() | ||
if err != nil { | ||
fmt.Printf("Request error: %s\n", err) | ||
return | ||
} | ||
if !res.Ok { | ||
fmt.Printf("Invalid server response: %d\n", res.StatusCode) | ||
return | ||
} | ||
|
||
// Set sample context data | ||
fmt.Println(req.Context.GetString("foo")) | ||
fmt.Println(req.Context.GetString("bar")) | ||
|
||
// Reads the whole body and returns it as string | ||
fmt.Printf("Body: %s", res.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters