I runned the bellow code to retrieve a json from api and parse the values to a struct to use the values, but the print appears in blank.
The json format is:
"user":{
"id":13,
"name":"xpto",
"email":"bla@blum.com.br",
"role":"partner"
},
"token":"hjhfsdhfjkhskfjhsjkfhjksdhfjkshfkjsoiwwsnskcnksjcjkscksjcksbsdsdjsdj"
}
and the code that i am running is:
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type resposta struct {
usuario usuario `json:"user"`
token string `json:"token"`
}
type usuario struct {
id string `json:"id"`
name string `json:"name"`
email string `json:"email"`
role string `json:"role"`
}
func main() {
values := map[string]string{"email": "blablum@xpto.com.br", "password": "senhaaaaa"}
jsonValue, _ := json.Marshal(values)
response, _ := http.Post("https://api.endereco.com.br/sessions", "application/json", bytes.NewBuffer(jsonValue))
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
println(body)
println(string(body))
var result resposta
json.Unmarshal(body, &result)
println("Resposta")
println(result.token)
println(result.token)
}
Any suggestion?