Http Post reponse return nil

Hello Community.
I was trying to get the token on Paypal with a http (module net/http) Post request.
The link to example: example
I tried decode and unmarshall and any way found on the internet.

respBody, err := ioutil.ReadAll(resp.Body)
json.Unmarshal([]byte(respBody), &info) // inf == []
json.NewDecoder(resp.Body).Decode(&res) // res == nil

I could see the bytes as int or the string converted but not the json struct.
I tried to use every of these:

var res interface{}
var res []interface{}
var res map[string]interface{}

I am new to golang.

Hello

I think you’re getting confused with json.
I don’t understand why about some of your stuff.
Here is a valid example: How to Parse JSON in Golang? - GeeksforGeeks

Anyway bytes should be converted in string with string(var)

I want to get the response of the post request.
Simple the result always is empty == [] or == nil.
Ex.

resp, err := http.NewRequest("POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", (strings.NewReader(data.Encode())))
respBody, err := io.ReadAll(resp.Body) // string(respBody) is "grant_type=client_credentials"

var info []interface{}
json.Unmarshal([]byte(string(respBody)), &info) // info is []

var res interface{}
var someErr = json.NewDecoder(resp.Body).Decode(&res) // res and someErr are <nil>

If i use map[string]interface{} result is map[].
So i guess the response gives the default value of the type.
Expected result should be an object(struct) with access token and other fields.
I saw the examples of how to unmarshal json and so on.
What i am missing.

is not valid JSON syntax

By itself it is a valid JSON string-value.

Though its hard to tell from the inline comment, whter or net string(respBody) is the JSON value "foo=bar" (go "\"foo=bar\"") or the go value "foo=bar"

I don’t think it has the quotes internally to make it an actual JSON string, but since he’s trying to parse as a map, a string is likely not what he expects.

Thanks all for the help.
I gave it a look again and i was doing it wrong.
I saw some examples in github and realized i was using the req and resp wrong.
So i changed this req, err := http.NewRequest("POST", "https://api-m.sandbox.paypal.com/v1/oauth2/token", (strings.NewReader(data.Encode())))
To do this

client := &http.Client{}
res, _ := client.Do(req)

and last

json.NewDecoder(res.Body).Decode(&info)

For clarification: I was passing

"grant_type=client_credentials"

correctly to the req, but not trying to get a response, so i had to do

res, _ := client.Do(req)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.