Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{}
example.com

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading