英文:
Programmatically replaying a raw HTTP request
问题
我有一些以文本文件形式保存的原始POST
和GET
请求。例如:
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
我想从文本文件中加载这个原始的HTTP请求,并以编程方式重新发送它。我考虑逐行读取请求,并为每一行设置一个头部,使用req.Header.Set("name", "value")
。但是当我尝试解析包含JSON主体的POST
请求时,事情变得有些奇怪。
有没有一种简单直接的方法来做到这一点?
英文:
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?
答案1
得分: 3
这似乎可以实现:
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
英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论