i finaly have it :D in fact, the whole struct of httpbin returns have to be
describe through different structures to works.
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
type httpbin struct {
Origin string `json:"origin"`
Headers map[string]string `json:"headers"`
Myuser User `json:"json"`
}
u := User{Firstname: "doy", Lastname: "nic"}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(u)
res, _ := http.Post("https://httpbin.org/post", "application/json;
charset=utf-8", b)
newhttpbin := httpbin{}
json.NewDecoder(res.Body).Decode(&newhttpbin)
fmt.Println(newhttpbin.Myuser)
}
Le mardi 14 août 2018 20:15:21 UTC+2, [email protected] a écrit :
>
> when i try something like that:
>
> res, _ := http.Post("https://httpbin.org/post", "application/json;
> charset=utf-8", b)
> var newuser map[string]interface{}
> json.NewDecoder(res.Body).Decode(&newuser)
> fmt.Println(newuser["json"])
>
> it prints me correctly the content a map containing the firstname &
> lastname
>
> Le mardi 14 août 2018 12:19:20 UTC+2, [email protected] a écrit :
>>
>> Hello
>>
>> I need some assistance to decode a json content
>>
>> I tried to use only usefull code to do it but don't success to retrieve
>> the data part of the json result.
>>
>> var user struct {
>> Firstname string `json: "firstname"`
>> Lastname string `json: "lastname"`
>> Origin string `"origin"`
>> }
>>
>> jsonData := map[string]string{"firstname": "Nic", "lastname": "Raboy"
>> }
>> jsonValue, _ := json.Marshal(jsonData)
>> response, err := http.Post("https://httpbin.org/post",
>> "application/json", bytes.NewBuffer(jsonValue))
>> defer response.Body.Close()
>> json.NewDecoder(response.Body).Decode(&user)
>> fmt.Println(user)
>>
>> but this code only map the origin field not the firstname, and lastname
>> part.
>>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.