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) *Request
Use 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{}) *Request
Use 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.
Hello @imroc and the
reqteam,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
reqlibrary, inspired by common use cases and existing patterns in other libraries likeresty.1. Add
SetQueryParamsFromValuesMethodMotivation
The
restylibrary provides aSetQueryParamsFromValuesmethod (reference: [resty documentation](https://resty.dev/docs/request-query-params/#query-params-from-urlvalues)), which allows users to directly set query parameters using aurl.Valuesmap. This is a common and convenient approach, especially when working with libraries likego-querystring(https://github.com/google/go-querystring). Thego-querystringlibrary can serialize a struct into aurl.Valuesmap, which is often used to construct query parameters.Currently, to achieve this in
req, users must convert a struct tourl.Valuesusinggo-querystring, decode theurl.Valuesinto a raw query string, and then pass it toSetQueryString. This process is cumbersome and error-prone. ASetQueryParamsFromValuesmethod would streamline this workflow by allowing direct configuration of query parameters from aurl.Valuesmap.Proposed API
Use Case Example
This would append
?name=example&limit=10to the request URL, making the process more straightforward.2. Add
SetQueryParamsFromStructMethodMotivation
Building on the
SetQueryParamsFromValuesmethod, aSetQueryParamsFromStructmethod would provide an even higher-level abstraction by allowing users to directly pass a struct to configure query parameters. This would internally leveragego-querystringto convert the struct tourl.Valuesand then useSetQueryParamsFromValuesto set the query parameters. This approach would further simplify the API for users who prefer working directly with structs.Proposed API
Use Case Example
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.