resty icon indicating copy to clipboard operation
resty copied to clipboard

Support query parameters without encoding

Open yangzhiw opened this issue 1 year ago • 4 comments

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)

yangzhiw avatar May 16 '24 07:05 yangzhiw

@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.

jeevatkm avatar May 16 '24 18:05 jeevatkm

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'

ahuigo avatar Jun 14 '24 16:06 ahuigo

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.

antixcode6 avatar Jun 27 '24 15:06 antixcode6

@antixcode6 I will add it in v3

jeevatkm avatar Jun 28 '24 03:06 jeevatkm

I am interested in this functionality as well since I'm dealing with legacy servers that cannot handle escaped query parameters at the moment. Can we add a function SetEscapeQueryParams(bool b) into v2 just like we have SetJSONEscapeHTML currently? This would really help improve the flexibility of the client for different requests

takanuva15 avatar Sep 11 '24 21:09 takanuva15

@takanuva15 Is it an urgent need? As you can see, I marked this issue for v3, which is currently in development.

jeevatkm avatar Sep 12 '24 00:09 jeevatkm

@jeevatkm no it's not urgent; thanks for your quick reply

takanuva15 avatar Sep 13 '24 15:09 takanuva15

@yangzhiw @antixcode6 @takanuva15 - I have implemented this in Resty v2, in the branch unescape-query-params. Can you try and share your testing feedback?

jeevatkm avatar Sep 21 '24 03:09 jeevatkm