Skip to content

Instantly share code, notes, and snippets.

@brandur
Created August 10, 2018 22:52
Show Gist options
  • Save brandur/c9f52a913487c13ecdfd6d2a329c233b to your computer and use it in GitHub Desktop.
Save brandur/c9f52a913487c13ecdfd6d2a329c233b to your computer and use it in GitHub Desktop.
Request re-use
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
client := &http.Client{}
buf := bytes.NewBufferString("hello, world")
req, err := http.NewRequest(http.MethodPost, "http://httpbin.org/anything", bytes.NewReader(buf.Bytes()))
if err != nil {
panic(err)
}
for i := 0; i < 10; i++ {
resp, err := client.Do(req)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("resp status = %+v body = %+v\n", resp.StatusCode, string(body))
}
}
@brandur
Copy link
Author

brandur commented Aug 10, 2018

Result:

$ go run main.go
resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.116",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.118",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.118",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.116",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.115",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.116",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.115",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.115",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.117",
  "url": "http://httpbin.org/anything"
}

resp status = 200 body = {
  "args": {},
  "data": "hello, world",
  "files": {},
  "form": {},
  "headers": {
    "Accept-Encoding": "gzip",
    "Connection": "close",
    "Content-Length": "12",
    "Host": "httpbin.org",
    "User-Agent": "Go-http-client/1.1"
  },
  "json": null,
  "method": "POST",
  "origin": "8.21.168.118",
  "url": "http://httpbin.org/anything"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment