Naresh
Naresh

Reputation: 5397

Extract case-insensitive query parameter from URL

I am trying to extract the case-insensitive query parameter /staging/ec/23463/front-view-72768.jpg?angle=90&or=0x0&wd=400&ht=200 from the URL. When I try to convert the whole URL in lowercase it throws the following exception :

cannot use r.URL (type *url.URL) as type string in argument to strings.ToLower

I printed the value of URL which says underlying it stores all the query strings as map i.e. map[angle:[90] or:[0x0] wd:[400] ht:[200]]. Hence I will get the correct value using this r.URL.Query().Get("or") But if query string comes out Or. It will fail.

Upvotes: 1

Views: 3931

Answers (2)

Peter
Peter

Reputation: 31691

*URL.Query() returns a value of type url.Values, which is just a map[string][]string with a few extra methods.

Since URL values are by definition case-sensitive, you will have to access the map directly.

var query url.Values

for k, vs := range query {
    if strings.ToLower(k) == "ok" {
        // do something with vs
    }
}

Try it on the playground: https://play.golang.org/p/7YVuxI3GO6X

Upvotes: 7

vedhavyas
vedhavyas

Reputation: 1151

cannot use r.URL (type *url.URL) as type string in argument to strings.ToLower

This is because you are passing ur.URL instead of string. Get the string from url through String() function.

url.String()

Upvotes: 0

Related Questions