go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.68k stars 681 forks source link

Support query parameters without encoding #797

Open yangzhiw opened 2 months ago

yangzhiw commented 2 months ago

When calling the SetQueryParam method, the request request will encode the query in the URL, for example: registry=nacos://test:6801, which will be encoded as: registry=nacos%3A%2F%2Ftest%3A6801; is there any way? To set query parameters without encoding, for example: SetQueryEscape(false)

jeevatkm commented 2 months ago

@yangzhiw Thanks for reaching out. Without query escape, the URL may become broken. It is not recommended to do that. Ideally, the application/service endpoint receiving this parameter should unescape first before using it.

ahuigo commented 1 month ago

try to put unencoded query to url like this:

    url := ts.URL + "/post?un-escape=nacos://test:6801"
    resp, err := req.
        EnableTrace().
        SetQueryParams(map[string]string{
            "escape":"nacos://test:6801",
        }).
        Post(url)

//  curl -X POST 'http://127.0.0.1:51301/post?unescape=nacos://test:6801&escape=nacos%3A%2F%2Ftest%3A6801'
antixcode6 commented 3 weeks ago

This is unfortunately something I have ran into recently as well.

Ideally, the application/service endpoint receiving this parameter should unescape first before using it

While I agree that they should Some APIs that I've been trying to consume recently refuse to un-encode the request on their end and will only accept raw, unencoded, query params. I believe giving the package consumer/programmer the option to decide this behavior makes more sense than relying on the service (which we most likely have no control over) to do the right thing.

jeevatkm commented 3 weeks ago

@antixcode6 I will add it in v3