英文:
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
{}
example.com
为什么请求体是空的?我如何从文件中解析完整的 POST 请求(包括请求体)?
英文:
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?
答案1
得分: 1
请求不完整。缺少一个Content-length头部,该头部覆盖了请求体。如果没有这个头部,就会假设请求体为空。在你的具体示例中,将Content-length: 10
添加到请求头部即可完成任务。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论