Documentation
¶
Overview ¶
Package httpfile provides HTTP request file(.http) parsing utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPFile ¶
type HTTPFile struct {
// FilePath is the path of the HTTP request file.
FilePath string
Contents string // the contents of the HTTP request file
Requests []*HTTPRequest
}
HTTPFile represents an HTTP request file. It contains a list of HTTP requests.
文件内容格式:
- 每个HTTP请求以方法、URL和空行开始
- 头部键值对每行一个,格式为 "Key: Value"
- 空行后为请求体(可选) `@filename` 可以指定请求体内容从文件中读取
- 可以使用变量替换请求中的内容,格式为 `${var_name}`
- 每个请求之间用 `空行+###开头的行` 分隔
- 单个 # 开头的行是注释,会被忽略
func ParseFileContent ¶
ParseFileContent parse a HTTP request file content.
func ParseHTTPFile ¶
ParseHTTPFile parse a HTTP request file.
func (*HTTPFile) FindByName ¶
func (hf *HTTPFile) FindByName(name string) *HTTPRequest
FindByName find an HTTP request by name.
func (*HTTPFile) SearchName ¶
func (hf *HTTPFile) SearchName(keywords ...string) []*HTTPRequest
SearchName search requests by keywords. will return all requests that name contains all keywords.
func (*HTTPFile) SearchOne ¶
func (hf *HTTPFile) SearchOne(keywords ...string) *HTTPRequest
SearchOne search one request by keywords. will return the first request that name contains all keywords.
- if no keywords, will return the first request.
type HTTPRequest ¶
type HTTPRequest struct {
// Name is the name of the HTTP request. parsed from ### line
Name string
Comments []string
// Method is the HTTP method of the request.
Method string
URL string
Headers map[string]string
Body string
}
HTTPRequest represents an HTTP request.
- URL, Headers, Body 可以包含变量,格式为 `${var_name}`
func ParseRequest ¶
func ParseRequest(content string) (*HTTPRequest, error)
ParseRequest parse a HTTP request from content string.
func ParseRequestWithVars ¶
func ParseRequestWithVars(content string, varMap map[string]string) (*HTTPRequest, error)
ParseRequestWithVars parse a HTTP request from content string.
func (*HTTPRequest) ApplyVars ¶
func (req *HTTPRequest) ApplyVars(varMap map[string]string)
ApplyVars apply variables to the HTTP request.
func (*HTTPRequest) BodyString ¶
func (req *HTTPRequest) BodyString(varMap map[string]string) string
BodyString get the body of the HTTP request.
func (*HTTPRequest) HeadersMap ¶
func (req *HTTPRequest) HeadersMap(varMap map[string]string) map[string]string
HeadersMap get the headers of the HTTP request.