The Requests library simplifies the way you make HTTP requests in Go. It provides an easy-to-use interface for sending requests and handling responses, reducing the boilerplate code typically associated with the net/http
package.
Begin by installing the Requests library:
go get github.com/kaptinlin/requests
Creating a new HTTP client and making a request is straightforward:
package main
import (
"github.com/kaptinlin/requests"
"log"
)
func main() {
// Create a client using a base URL
client := requests.URL("http://example.com")
// Alternatively, create a client with custom configuration
client = requests.Create(&requests.Config{
BaseURL: "http://example.com",
Timeout: 30 * time.Second,
})
// Perform a GET request
resp, err := client.Get("/resource")
if err != nil {
log.Fatal(err)
}
defer resp.Close()
log.Println(resp.String())
}
The Client
struct is your gateway to making HTTP requests. You can configure it to your needs, setting default headers, cookies, timeout durations, and more.
client := requests.URL("http://example.com")
// Or, with full configuration
client = requests.Create(&requests.Config{
BaseURL: "http://example.com",
Timeout: 5 * time.Second,
Headers: &http.Header{
"Content-Type": []string{"application/json"},
},
})
For more details, see docs/client.md.
The library provides a RequestBuilder
to construct and dispatch HTTP requests. Here are examples of performing various types of requests, including adding query parameters, setting headers, and attaching a body to your requests.
To retrieve data from a specific resource:
resp, err := client.Get("/path").
Query("search", "query").
Header("Accept", "application/json").
Send(context.Background())
To submit data to be processed to a specific resource:
resp, err := client.Post("/path").
Header("Content-Type", "application/json").
JSONBody(map[string]interface{}{"key": "value"}).
Send(context.Background())
To replace all current representations of the target resource with the request payload:
resp, err := client.Put("/articles/{article_id}").
PathParam("article_id", "123456").
JSONBody(map[string]interface{}{"updatedKey": "newValue"}).
Send(context.Background())
To remove all current representations of the target resource:
resp, err := client.Delete("/articles/{article_id}").
PathParam("article_id", "123456").
Send(context.Background())
For more details, visit docs/request.md.
Handling responses is crucial in determining the outcome of your HTTP requests. The Requests library makes it easy to check status codes, read headers, and parse the body content.
Parsing JSON response into a Go struct:
type APIResponse struct {
Data string `json:"data"`
}
var apiResp APIResponse
if err := resp.ScanJSON(&apiResp); err != nil {
log.Fatal(err)
}
log.Printf("Status Code: %d\n", resp.StatusCode())
log.Printf("Response Data: %s\n", apiResp.Data)
This example demonstrates how to unmarshal a JSON response and check the HTTP status code.
For more on handling responses, see docs/response.md.
- Logging: Learn how to configure logging for your requests. See docs/logging.md.
- Middleware: Extend functionality with custom middleware. See docs/middleware.md.
- Retry Mechanism: Implement retry strategies for transient errors. See docs/retry.md.
- Stream Support: Utilize streaming for real-time data processing. See docs/stream.md.
This library was inspired by and built upon the work of several other HTTP client libraries:
Contributions to the requests
package are welcome. If you'd like to contribute, please follow the contribution guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.