2

I have some raw POST and GET requests in text files. For example:

GET /images HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Connection: close

I want to load this raw HTTP request from a text file and replay it programmatically. I'm thinking of reading the request line by line and set a header for each line with req.Header.Set("name", "value"). But things get weird when I try to parse a POST request that contains JSON body.

Is there a straightforward way to do this?

Robert Zunr
  • 67
  • 1
  • 11
  • 1
    Does this answer your question? [Parse HTTP requests and responses from text file in Go](https://stackoverflow.com/questions/33963467/parse-http-requests-and-responses-from-text-file-in-go) – Steffen Ullrich May 29 '21 at 19:52
  • @SteffenUllrich I don't think so – Robert Zunr May 29 '21 at 19:56
  • Then I don't understand your question. For me it looks like you want to read a HTTP request from a file, create a http.Request from it in order to replay it. That's exactly what the linked question is about, only that it also shows it for the http.Response too. If you instead want a specific help with the *"But things get weird when I try to parse a POST request that contains JSON body."*, then you would need to explain in more detail what you are actually doing (code!) and what exactly goes weird. – Steffen Ullrich May 29 '21 at 20:03
  • @SteffenUllrich your understanding is correct. However, the referenced question contains response parsing and that makes it hard to understand for me. – Robert Zunr May 29 '21 at 20:08

1 Answers1

3

This seems to do it:

package main

import (
   "bufio"
   "net/http"
   "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() {
   raw := `GET /images HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Connection: close

`
   req, err := readRequest(raw, "http")
   if err != nil {
      panic(err)
   }
   new(http.Client).Do(req)
}

https://golang.org/pkg/net/http#ReadRequest

Zombo
  • 1
  • 62
  • 391
  • 407