Request Bodies
Request bodies can be set when making a request. The HTTP client supports various types of request bodies.
JSON
JSON request bodies can be set using the RequestBodyJSON
helper function which will json.Marshal
the provided value.
Strongly Typed
httpc := httpr.NewClient()
type Post struct { UserID int `json:"userId"` ID int `json:"id"` Title string `json:"title"` Body string `json:"body"`}
post := Post{ UserID: 1, ID: 1, Title: "foo", Body: "bar",}
resp, err := httpc.Post( context.Background(), "https://jsonplaceholder.typicode.com/posts", httpr.RequestBodyJSON(post),)
Map
httpc := httpr.NewClient()
data := map[string]any{ "userId": 1, "id": 1, "title": "foo", "body": "bar",}
resp, err := httpc.Post( context.Background(), "https://jsonplaceholder.typicode.com/posts", httpr.RequestBodyJSON(post),)
String
String request bodies can be set using the RequestBodyString
helper function.
httpc := httpr.NewClient()
resp, err := httpc.Post(context.Background(), "https://api.example.com/text", httpr.RequestBodyString("Hello, World!"),)
Form Data
Form data request bodies can be set using the RequestBodyForm
helper function.
httpc := httpr.NewClient()
formData := url.Values{}formData.Add("username", "johndoe")formData.Add("password", "secret")
resp, err := httpc.Post(context.Background(), "https://api.example.com/form", httpr.RequestBodyForm(formData),)
Bytes
Bytes request bodies can be set using the RequestBodyBytes
helper function.
httpc := httpr.NewClient()
myBytes := []byte{0x00, 0x01, 0x02, 0x03}resp, err := httpc.Post(context.Background(), "https://api.example.com/binary", httpr.RequestBodyBytes("application/octet-stream", myBytes),)
Streaming Request Body
If you need to send a large amount of data that you don’t want to load into memory all at once, use the RequestBodyStream
helper function.
httpc := httpr.NewClient()
fileReader, err := os.Open("large_file.dat")if err != nil { // Handle error}defer fileReader.Close()
resp, err := httpc.Post(context.Background(), "https://api.example.com/upload", httpr.RequestBodyStream("application/octet-stream", fileReader),)
Custom Request Body Helper
httpc := httpr.NewClient()
customBody := httpr.RequestBody("application/custom", func() (io.Reader, error) { // Your custom logic here return strings.NewReader("Custom data"), nil})
resp, err := httpc.Post(context.Background(), "https://api.example.com/custom", customBody,)