Created
November 28, 2018 10:13
-
-
Save bussiere/e38f45ef74c15a8e6ba15315ba6d48cb to your computer and use it in GitHub Desktop.
Send json with go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
var reception = Data{} | |
var sendData = Data{} | |
sendData.Fruit = "Melon" | |
url := "http://localhost:8309/" | |
fmt.Println("URL:>", url) | |
js, err := json.Marshal(sendData) | |
req, err := http.NewRequest("GET", url, bytes.NewBuffer(js)) | |
req.Header.Set("X-Custom-Header", "myvalue") | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
fmt.Println("response Status:", resp.Status) | |
fmt.Println("response Headers:", resp.Header) | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println("response Body:", string(body)) | |
err = json.Unmarshal(body, &reception) | |
fmt.Println(reception.Fruit) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment