Skip to content

Instantly share code, notes, and snippets.

@airtonGit
Created May 28, 2019 00:36
Show Gist options
  • Save airtonGit/151175e455b314f18b2286a16e348b5e to your computer and use it in GitHub Desktop.
Save airtonGit/151175e455b314f18b2286a16e348b5e to your computer and use it in GitHub Desktop.
Simple GO json request
package main
import (
"fmt"
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
func main(){
reqBody, err := json.Marshal(map[string]string{
"name": "Nome Extenso",
"email": "email@provedor.com",
})
if err != nil {
panic(err.Error())
}
cancTimeout = time.Duration(10 * time.Second)
client := http.Client{
Timeout: cancTimeout,
}
request, err := http.NewRequest("POST","http://localhost/servico", bytes.NewBuffer(requestBody))
request.Header.Set("Content-Type", "application/json")
if err != nil {
panic(err.Error())
}
resp, err := client.Do(request)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
body,err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
fmt.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment