Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode url parameters in golang?

Tags:

go

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?

like image 267
shaidiren Avatar asked Sep 12 '16 04:09

shaidiren


People also ask

How do I encode URL in Golang?

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.

How do I decode URL in Golang?

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.

How do I encode strings in Golang?

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.

What does URL parse do in Golang?

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.


1 Answers

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())

}
like image 198
shaidiren Avatar answered Oct 08 '22 21:10

shaidiren