V
V
Vadim Rublev2020-12-08 09:51:17
go
Vadim Rublev, 2020-12-08 09:51:17

How to extract GET parameter (token) from URL?

The Go server receives a URL with a GET parameter (this is a token) -

https://site.com/Link?$2a$10$/VTT.GtRslGWLhZL5dFoZODGwJxppnCM5

How can I retrieve this token as a string (string type) or a byte (byte type)?

So it extracts, but I don’t understand how to bring it to the desired type.
var valueGET = r.URL.Query()   // map[$2a$10$/VTT.GtRslGWLhZL5dFoZODGwJxppnCM5:[]]


Or is there a better function/way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2020-12-08
@Vadim Rublev

For the Query function to work the way you want - the URL must be like ?param1=value1¶m2=value2, then you can extract values ​​from the map by the keys param1, param2, etc.
I think in your case it is better to use just Split or another suitable function from the strings package.
Here is a working example with Split.

package main

import (
    "fmt"
    "strings"
)

func main() {
    url := `https://site.com/regLink?$2a$10$/VTT.GtRslGWLhZL5d`
    result := strings.SplitN(url, `?`, 2)
    fmt.Printf("token: %s\n", result[1])
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question