5

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?

2
  • Please add how the generated URL should look like. Currently the question is too vague. Commented Sep 12, 2016 at 6:49
  • host:port/path?para={"key1": value1,"key2":"value2"}, I want to encode the parameter(in map structure) and transport it with json_encoded @RolandIllig Commented Sep 13, 2016 at 3:16

2 Answers 2

5

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

Here you can view how to urlencode a 'key-set' for send a request (like query params or form values)

import "net/url"
encoded := url.Values{}
encoded.Set("grant_type", "urn:ibm:params:oauth:grant-type:apikey")
encoded.Set("apikey", conf.Apikey)

For further information have a look here: https://github.com/alessiosavi/GoCloudant/blob/a8ad3a7990f04ea728bb327d6faea6af3e5455ca/cloudant.go#L117

Here a few-lines-library that wrap the builtin library for execute HTTP request used in the previous code: https://github.com/alessiosavi/Requests

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.