In url crate, how do I construct query params without the library encoding them to urlencoded format?

I am using the url crate. I parse a string into Url and then I want to add query parameters to this URL.
I am trying to do it like this,


        let query_params = [
            ("client_id", self.client_id.as_str()),
            ("scope", "read,read_all,profile:read_all,profile:write,profile:write,activity:read,activity:read_all,activity:write"),
            ("redirect_uri", self.redirect_uri.as_str()),
            ("approval_prompt", "force"),
            ("response_type", "code")
        ];

        for (p, q) in query_params {
            url.query_pairs_mut().append_pair(p, q);
        }

but the url crate is encoding the query parameters into urlencode format so the url looks like this,

https://www.strava.com/oauth/authorize?client_id=169855&scope=read%2Cread_all%2Cprofile%3Aread_all%2Cprofile%3Awrite%2Cprofile%3Awrite%2Cactivity%3Aread%2Cactivity%3Aread_all%2Cactivity%3Awrite&redirect_uri=https%3A%2F%2Fstrava.ishanjain.me%2Fauth_callback&approval_prompt=force&response_type=code

Is there a nicer way to do this without going back to creating the query parameter string myself and setting it with set_query ?

I am just venting a little bit but the url crate has such a horrible API I dread every single time I have to do any thing with it.

The URL is stored percent encoded. Even if you construct the query string yourself, url will encode it the moment you try to add it to the url, even with Url::set_query. Instead you could retrieve the percent-encoded query part from the Url and decode it yourself before printing. AFAIK url does not offer an API for this, you'd have to do so yourself, e.g. with the percent-encoding crate.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.