Skip to content

lib4u/grequest

Repository files navigation

logo

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.


Features

  • 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.

Installation

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

Usage

Basic GET Request

req := app.Get("https://jsonplaceholder.typicode.com/todos/1").Do()
fmt.Println(req.Body().GetStrings())

Parsing JSON Response

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)

POST Request with JSON Payload

data := LoginRequest{
    Username: "example",
    Password: "12345",
}
req := app.Post("https://example.site/login").Body().SetJson(data).Do()
fmt.Println(req.Status().GetCode())

Downloading a File

app.Get("https://example.com/image.png").Do().Body().SaveFile()

Multipart Form Submission

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()

Set count of maximum redirects and retry request if get 404 or 500 HTTP code

req := app.Get("https://jsonplaceholder.typicode.com/todos/1").MaxRedirect(1).RetryIf(404, 500).Do()
fmt.Println(req.Body().GetStrings())

Set your context

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())

Authenticated Requests

// 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()

Cookie Handling

//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()

Contributing

  1. Fork the repository and clone it locally.
  2. Create a new branch (git checkout -b feature/branch-name).
  3. Make your changes and commit (git commit -m "Description of changes").
  4. Push your changes and create a pull request.

License

Grequests is licensed under the MIT License. See the LICENSE file for details.

For more details, visit the GitHub repository.