-
Notifications
You must be signed in to change notification settings - Fork 386
Description
Hello @imroc and the req team,
Thank you for maintaining this excellent HTTP client library for Go. It’s been incredibly useful due to its simplicity and powerful features. I would like to propose two new methods to enhance the query parameter configuration functionality in the req library, inspired by common use cases and existing patterns in other libraries like resty.
1. Add SetQueryParamsFromValues Method
Motivation
The resty library provides a SetQueryParamsFromValues method (reference: [resty documentation](https://resty.dev/docs/request-query-params/#query-params-from-urlvalues)), which allows users to directly set query parameters using a url.Values map. This is a common and convenient approach, especially when working with libraries like go-querystring (https://github.com/google/go-querystring). The go-querystring library can serialize a struct into a url.Values map, which is often used to construct query parameters.
Currently, to achieve this in req, users must convert a struct to url.Values using go-querystring, decode the url.Values into a raw query string, and then pass it to SetQueryString. This process is cumbersome and error-prone. A SetQueryParamsFromValues method would streamline this workflow by allowing direct configuration of query parameters from a url.Values map.
Proposed API
// SetQueryParamsFromValues sets query parameters from a url.Values map.
func (r *Request) SetQueryParamsFromValues(params url.Values) *RequestUse Case Example
import (
"github.com/google/go-querystring/query"
"github.com/imroc/req/v3"
"net/url"
)
type QueryParams struct {
Name string `url:"name"`
Limit int `url:"limit"`
}
func main() {
client := req.C()
params := QueryParams{
Name: "example",
Limit: 10,
}
values, _ := query.Values(params) // Convert struct to url.Values using go-querystring
client.R().SetQueryParamsFromValues(values).Get("https://example.com/api")
}This would append ?name=example&limit=10 to the request URL, making the process more straightforward.
2. Add SetQueryParamsFromStruct Method
Motivation
Building on the SetQueryParamsFromValues method, a SetQueryParamsFromStruct method would provide an even higher-level abstraction by allowing users to directly pass a struct to configure query parameters. This would internally leverage go-querystring to convert the struct to url.Values and then use SetQueryParamsFromValues to set the query parameters. This approach would further simplify the API for users who prefer working directly with structs.
Proposed API
// SetQueryParamsFromStruct sets query parameters from a struct using go-querystring.
func (r *Request) SetQueryParamsFromStruct(v interface{}) *RequestUse Case Example
import (
"github.com/imroc/req/v3"
)
type QueryParams struct {
Name string `url:"name"`
Limit int `url:"limit"`
}
func main() {
client := req.C()
params := QueryParams{
Name: "example",
Limit: 10,
}
client.R().SetQueryParamsFromStruct(params).Get("https://example.com/api")
}This would produce the same result as the previous example (?name=example&limit=10) but with a simpler, more idiomatic API.
I’d be happy to discuss the implementation details further or even contribute a pull request if this proposal aligns with the project’s roadmap. Thank you for considering this feature request, and please let me know if you need any clarification!
Best regards.