http.ReadRequest无法解析来自文件的POST请求的请求体。

huangapple go评论130阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年8月8日 17:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73275425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定