A simple and lightweight HTTP client for Go, inspired by Requests (Python) and Guzzle (PHP).
Grequests provides a declarative, chainable API to streamline HTTP requests in Go, supporting JSON manipulation, form submissions, cookies, file handling, authentication, and proxy configuration—all while remaining lightweight and dependency-free.
- Lightweight & Efficient: No third-party dependencies, built directly on
net/http
. - Flexible Request Handling: Supports GET, POST, PUT, DELETE, and other HTTP methods.
- JSON Parsing: Convert responses into Go structs or string maps.
- Header & Cookie Management: Easily set, retrieve, and persist headers and cookies.
- File Handling: Upload and download files seamlessly.
- Authentication: Supports Basic, Bearer, and custom token authentication.
- Proxy Support: Configure custom proxy servers for HTTP requests.
Install Grequests using Go modules:
go get github.com/lib4u/grequest
Or build from source:
git clone https://github.com/lib4u/grequest.git
cd grequest
go build -o grequest ./
./grequest --version
req := app.Get("https://jsonplaceholder.typicode.com/todos/1").Do()
fmt.Println(req.Body().GetStrings())
type Todo struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
req := app.Get("https://jsonplaceholder.typicode.com/todos/1").Do()
err := req.Body().GetWithJsonStruct(&todo)
if err != nil {
fmt.Println("Error decoding JSON")
}
fmt.Println(todo.Title)
data := LoginRequest{
Username: "example",
Password: "12345",
}
req := app.Post("https://example.site/login").Body().SetJson(data).Do()
fmt.Println(req.Status().GetCode())
app.Get("https://example.com/image.png").Do().Body().SaveFile()
req := app.Post("https://example.site/form/")
req.Header().Set("Client", "number_1")
form := req.FormData().WithMultipart()
form.AddField("first_name", "John")
form.AddFile("photo", "my_photo.png")
form.Push()
req.Do()
req := app.Get("https://jsonplaceholder.typicode.com/todos/1").MaxRedirect(1).RetryIf(404, 500).Do()
fmt.Println(req.Body().GetStrings())
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
req := app.Get("https://jsonplaceholder.typicode.com/todos/1").DoWithContext(ctx)
fmt.Println(req.Body().GetStrings())
// Basic Authentication
app.Post("https://example.site/secret").Auth().SetBasic("user", "password").Do()
// Bearer Token Authentication
app.Post("https://example.site/secret").Auth().SetBearer("myToken").Do()
// Custom Token Authentication
app.Post("https://example.site/secret").Auth().SetToken("Token", "myToken").Do()
// Custom Header Authentication
app.Post("https://example.site/secret").Auth().SetHeader("JSESSIONID", "12345").Do()
//Save cookie to file
//By default this saved in cookies/example.site/cookies.json
req := app.Post("https://example.site/cookies")
req.Cookie().Save()
// Load saved cookies form cookies/example.site/cookies.json
reqWithCookie := app.Post("https://example.site/cookies")
reqWithCookie.Cookie().Load()
reqWithCookie.Do()
// Clear cookies
reqWithCookie.Cookie().Clear()
- Fork the repository and clone it locally.
- Create a new branch (
git checkout -b feature/branch-name
). - Make your changes and commit (
git commit -m "Description of changes"
). - Push your changes and create a pull request.
Grequests is licensed under the MIT License. See the LICENSE file for details.
For more details, visit the GitHub repository.