Is there any go package to encode url?I need to encode a parameter(type in map[string]interface{}) before it was transfered in url.
Maybe the parameter likes:map[string]interface{}{"app_id":"you_api","app_sign":"md5_base_16","timestamp":"1473655478000"}
How to encode it and what the encoded result would be?
URL Encoding or Escaping a String in Go Go provides the following two functions to encode or escape a string so that it can be safely placed inside a URL: QueryEscape(): Encode a string to be safely placed inside a URL query string. PathEscape(): Encode a string to be safely placed inside a URL path segment.
URL encoding or percent-encoding, is a method of encoding URLs using only a limited set of characters so that the URL can be transmitted safely over the Internet. To decode such an encoded URL in Go, you can just use the parsing URL function url. Parse() from the url package. It parses and decodes all parts of the URL.
To encode a string to Base64 in Go, use the EncodeToString() function from the encoding/base64 standard library package. This function takes a byte slice and converts it to a Base64 encoded string, but it can also be used for a string argument by converting it to a byte slice.
Go has good support for url parsing. URL contains a scheme, authentication info, host, port, path, query params, and query fragment. we can parse URL and deduce what are the parameter is coming to the server and then process the request accordingly.
There is the one of method to get it.
package main
import (
"fmt"
"net/url"
"encoding/json"
)
func main() {
m := map[string]interface{}{"app_id": "you_api", "app_sign": "md5_base_16", "timestamp": "1473655478000"}
json_str, _ := json.Marshal(m)
fmt.Println(string(json_str[:]))
values := url.Values{"para": {string(json_str[:])}}
fmt.Println(values.Encode())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With