Created
May 28, 2019 00:36
-
-
Save airtonGit/151175e455b314f18b2286a16e348b5e to your computer and use it in GitHub Desktop.
Simple GO json request
This file contains 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
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