Home > OS >  http.ReadRequest does not parse body of POST Request from file
http.ReadRequest does not parse body of POST Request from file

Time:08-08

request.txt

POST /login HTTP/1.1
Host: example.com
User-Agent: curl/7.81.0
Accept: */*

pass=112233

CODE

req, err := ioutil.ReadFile("./request.txt")

if err != nil {
    log.Fatal(err)
}

reqbuf := bufio.NewReader(strings.NewReader(string(req)))

myreq, err := http.ReadRequest(reqbuf)
if err != nil {
    log.Fatal(err)
}
fmt.Println(myreq.Body)
fmt.Println(myreq.Host)

OUTPUT

{}
example.com

Why body is empty? How can I parse full POST request (including body of request) from file?

CodePudding user response:

The requests is incomplete. It is missing a Content-length header which covers the body. Without this it is assumed that the body is empty. In your specific example adding Content-length: 10 to the request header does the job.

  • Related