Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Created February 15, 2020 08:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ReeganExE/af2259bcdec3e8535bfaaa86a2c03e39 to your computer and use it in GitHub Desktop.
Save ReeganExE/af2259bcdec3e8535bfaaa86a2c03e39 to your computer and use it in GitHub Desktop.
Sample POST json in Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type Student struct {
Name string `json:"name"`
Address string `json:"address"`
}
func main() {
body := &Student{
Name: "abc",
Address: "xyz",
}
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, _ := http.NewRequest("POST", "https://httpbin.org/post", buf)
client := &http.Client{}
res, e := client.Do(req)
if e != nil {
log.Fatal(e)
}
defer res.Body.Close()
fmt.Println("response Status:", res.Status)
// Print the body to the stdout
io.Copy(os.Stdout, res.Body)
}
// response Status: 200 OK
// {
// "args": {},
// "data": "{\"name\":\"abc\",\"address\":\"xyz\"}\n",
// "files": {},
// "form": {},
// "headers": {
// "Accept-Encoding": "gzip",
// "Content-Length": "31",
// "Host": "httpbin.org",
// "User-Agent": "Go-http-client/1.1"
// },
// "json": {
// "address": "xyz",
// "name": "abc"
// },
// "origin": "118.127.110.2",
// "url": "https://httpbin.org/post"
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment