An easy-to-use HTTP request tool for Golang.
- Timeouts or self-control context.
- Serialize request body automatically.
- Response body deserialization wrapper.
This package requires Go 1.18 and later versions.
You can install this package by the following command.
go get -u github.com/ghosind/go-requestThe is a minimal example of performing a GET request:
resp, err := request.Request("https://dummyjson.com/products/1") if err != nil { // handle error } // handle responseYou can perform a POST request with a request config that set the Method field's value to POST.
resp, err := request.Request("https://dummyjson.com/products/add", RequestConfig{ Method: "POST", Body: map[string]any{ "title": "Apple", }, }) // handle error or responseIf the ContentType field in the request config is empty, the body data will serialize to a JSON string default, and it'll also set the Content-Type field value in the request headers to application/json.
You can also use POST method to perform a POST request with the specific body data.
resp, err := request.POST("https://dummyjson.com/products/add", RequestConfig{ Body: map[string]any{ "title": "Apple", }, }) // handle error or responseWe also provided the following methods for performing HTTP requests:
DELETEGETHEADOPTIONSPATCHPOSTPUT
The above methods will overwrite the
Methodfield in the request config.
All the requests will set timeout to 1-second default, you can set a custom timeout value in milliseconds to a request:
resp, err := request.Request("https://example.com", request.RequestConfig{ Timeout: 3000, // 3 seconds }) // handle error or responseYou can also set Timeout to request.RequestTimeoutNone to disable the timeout mechanism.
The timeout will be disabled if you set
Contextin the request config, you need to handle it manually.
We provided ToObject and ToString methods to handle response body. For example, the ToString method will read all data in the response body, and return it that represented in a string value.
content, resp, err := ToString(request.Request("https://dummyjson.com/products/1")) if err != nil { // handle error } // content: {"id":1,"title":"iPhone9",... // handle responseThe ToObject method will read the content type of response and deserialize the body to the specific type.
type Product struct { ID int `json:"id"` Title string `json:"title"` } product, resp, err := ToObject[Product](request.Request("https://dummyjson.com/products/1")) if err != nil { // handle error } // product: {1 iPhone9} // handle responseBoth
ToObjectandToStringmethods will close theBodyof the response after reading all data.
You can create a new client instance with a custom config.
cli := request.New(request.Config{ BaseURL: "https://dummyjson.com/", }) resp, err := cli.GET("/products/1") // handle error or response| Field | Type | Description |
|---|---|---|
BaseURL | string | The base url for all requests that performing by this client instance. |
Headers | map[string][]string | Custom headers to be sent. |
Timeout | int | Timeout in milliseconds. |
There are the available config options for performing a request, and all fields are optional.
| Field | Type | Description |
|---|---|---|
BaseURL | string | The base url for all requests that performing by this client instance. |
Parameters | map[string][]string | Custom query string parameters to be sent. |
Headers | map[string][]string | Custom headers to be sent. |
Timeout | int | Timeout in milliseconds. |
Context | context.Context | Self-control context. |
Body | any | The request body. |
Method | string | HTTP request method, default GET. |
ContentType | string | The content type of this request, default application/json if it's empty. |
- Request and response intercepts.
- URL encoded form serialization/deserialization.