以编程方式重放原始的HTTP请求

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

Programmatically replaying a raw HTTP request

问题

我有一些以文本文件形式保存的原始POSTGET请求。例如:

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)
}

https://golang.org/pkg/net/http#ReadRequest

huangapple
  • 本文由 发表于 2021年5月30日 03:44:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/67755205.html
匿名

发表评论

匿名网友

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

确定