Simple Go HTTP client with Black Magic
Simple Go HTTP client with Black Magic
Full documentation is available on the official website: https://req.cool.
HTTP/1.1, HTTP/2, and HTTP/3, and can automatically detect the server side and select the optimal HTTP version for requests, you can also force the protocol if you want (See Force HTTP version).req.Transport is exportable. Compared with http.Transport, it also supports HTTP3, dump content, middleware, etc. It can directly replace the Transport of http.Client in existing projects, and obtain more powerful functions with minimal code change.Install
You first need Go installed (version 1.24+ is required), then you can use the below Go command to install req:
go get github.com/imroc/req/v3Import
Import req to your code:
import "github.com/imroc/req/v3"Basic Usage
# assume the following codes in main.go file
$ cat main.gopackage main
import (
"github.com/imroc/req/v3"
)
func main() {
req.DevMode() // Treat the package name as a Client, enable development mode
req.MustGet("https://httpbin.org/uuid") // Treat the package name as a Request, send GET request.
req.EnableForceHTTP1() // Force using HTTP/1.1
req.MustGet("https://httpbin.org/uuid")
}…The sample code above is good for quick testing purposes, which use DevMode() to see request details, and send requests using global wrapper methods that use the default client behind the scenes to initiate the request.
In production, it is recommended to explicitly create a client, and then use the same client to send all requests, please see other examples below.
Videos
The following is a series of video tutorials for req:
More
Check more introduction, tutorials, examples, best practices and API references on the official website.
package main
import (
"fmt"
"github.com/imroc/req/v3"
"log"
)
func main() {
client := req.C() // Use C() to create a client.
resp, err := client.R(). // Use R() to create a request.
Get("https://httpbin.org/uuid")
if err != nil {
log.Fatal(err)
}
fmt.Println(resp)
}{
"uuid": "a4d4430d-0e5f-412f-88f5-722d84bc2a62"
}…Normally it will output (SuccessState):
roc (https://imroc.cc)You can set up a unified logic for error handling on the client, so that each time you send a request you only need to focus on the success situation, reducing duplicate code.
………If you like, you can also use a Do API style like the following to make requests:
package main
import (
"fmt"
"github.com/imroc/req/v3"
)
type APIResponse struct {
Origin string `json:"origin"`
Url string `json:"url"`
}
func main() {
var resp APIResponse
c := req.C().SetBaseURL("https://httpbin.org/post")
err := c.Post().
SetBody("hello").
Do().
Into(&resp)
if err != nil {
panic(err)
}
fmt.Println("My IP is", resp.Origin)
}My IP is 182.138.155.113Do() to fire the request, return Response, and finally call Response.Into to unmarshal response body into specified object.Response.Into will return an error if an error occurs during sending the request or during unmarshalling.Client.SetBaseURL can be used to set a unified url, and there is no need to set the url for each request when initiating a request. Of course, you can also call Request.SetURL to set it if you need it.Here is an example of building GitHub's SDK with req, using two styles (GetUserProfile_Style1, GetUserProfile_Style2).
…| Req Version | Go Version |
|---|---|
| 3.53.0 | 1.24+ |
| 3.51.0 | 1.23+ |
| 3.43.0 | 1.21+ |
| 3.41.0 | 1.20+ |
If you have a bug report or feature request, you can open an issue, and pull requests are also welcome.
If you have questions, feel free to reach out to us in the following ways:
If you like req and it really helps you, feel free to reward me with a cup of coffee, and don't forget to mention your github id.
|
Many thanks to the following sponsors:
|
Req released under MIT license, refer LICENSE file.
No open issues yet, or sync has not completed.