Help improve this documentation
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
HTTP Client operatorsโ
This page lists all operators available in the http/client
sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/http/client
HTTPRequestโ
Sends HTTP requests and returns the response.
import (
"fmt"
"net/http"
"github.com/samber/ro"
rohttp "github.com/samber/ro/plugins/http/client"
)
req, _ := http.NewRequest("GET", "https://api.example.com/users", nil)
obs := rohttp.HTTPRequest(req, nil)
sub := obs.Subscribe(ro.NewObserver(
func(resp *http.Response) {
defer resp.Body.Close()
fmt.Printf("Status: %s\n", resp.Status)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Status: 200 OK
// CompletedSimilar:Prototype:func HTTPRequest(req *http.Request, client *http.Client)
HTTPRequestJSONโ
Sends HTTP requests and automatically decodes JSON responses.
import (
"fmt"
"net/http"
"github.com/samber/ro"
rohttp "github.com/samber/ro/plugins/http/client"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
req, _ := http.NewRequest("GET", "https://api.example.com/users/1", nil)
obs := rohttp.HTTPRequestJSON[User](req, nil)
sub := obs.Subscribe(ro.NewObserver(
func(user User) {
fmt.Printf("User: {ID:%d Name:%s Email:%s}\n", user.ID, user.Name, user.Email)
},
func(err error) {
fmt.Printf("Error: %v\n", err)
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// User: {ID:1 Name:Alice Email:[email protected]}
// CompletedSimilar:Prototype:func HTTPRequestJSON[T any](req *http.Request, client *http.Client)