// You can edit this code! // Click here and start typing. package main import ( "encoding/base64" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "net/url" "strconv" "strings" ) func main() { data := url.Values{} data.Set("grant_type", "client_credentials") authentication := base64.StdEncoding.EncodeToString([]byte(config.Id + ":" + config.Secret)) // Buffer.from(`${id}:${secret}`).toString("base64") resp, err := http.NewRequest("POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", (strings.NewReader(data.Encode()))) // http.Post("https://api-m.sandbox.paypal.com/v1/oauth2/token", "application/x-www-form-urlencoded", strings.NewReader(data)) if err != nil { log.Printf("Request Failed: %s", err) // return "" } resp.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) resp.Header.Add("Authorization", "Basic "+authentication) defer resp.Body.Close() // first try respBody, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("someErr:", err) } fmt.Println(string(respBody)) // returns grant_type=client_credentials resJSon, err := json.Marshal(respBody) var info []interface{} json.Unmarshal([]byte(respBody), &info) fmt.Println(string(resJSon), info) // returns "Z3Jh..." and info = [] empty // second try var res interface{} var someErr = json.NewDecoder(resp.Body).Decode(&res) if someErr != nil { fmt.Println("someErr:", err) } fmt.Println("-------------------", res) // returns }