Skip to content

Instantly share code, notes, and snippets.

@fyxme
Last active February 16, 2024 15:58
Show Gist options
  • Save fyxme/25fb9bc7a3c76997ec51da30d3f3e4dd to your computer and use it in GitHub Desktop.
Save fyxme/25fb9bc7a3c76997ec51da30d3f3e4dd to your computer and use it in GitHub Desktop.
Replay an HTTP request from a txt file in golang
package main
// src: https://gist.github.com/fyxme/25fb9bc7a3c76997ec51da30d3f3e4dd
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"strings"
)
func readRequest(raw, scheme string) (*http.Request, error) {
r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(raw)))
if err != nil { return nil, err }
r.RequestURI, r.URL.Scheme, r.URL.Host = "", scheme, r.Host
return r, nil
}
func main() {
var infile string
flag.StringVar(&infile, "in", "", "(Input file with HTTP request to replay)")
flag.Parse()
if infile == "" {
fmt.Println("[!] Missing input file")
flag.PrintDefaults()
os.Exit(1)
}
data, err := ioutil.ReadFile(infile)
if err != nil {
fmt.Println("Can't read file:",infile)
panic(err)
}
req, err := readRequest(string(data), "http")
if err != nil {
panic(err)
}
resp, err := new(http.Client).Do(req)
if err != nil {
panic(err)
}
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
panic(err)
}
fmt.Printf(string(respDump))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment