Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Created February 15, 2020 08:00
Show Gist options
  • Select an option

  • Save ReeganExE/af2259bcdec3e8535bfaaa86a2c03e39 to your computer and use it in GitHub Desktop.

Select an option

Save ReeganExE/af2259bcdec3e8535bfaaa86a2c03e39 to your computer and use it in GitHub Desktop.

Revisions

  1. ReeganExE created this gist Feb 15, 2020.
    62 changes: 62 additions & 0 deletions post-json.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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"
    // }