Compare commits

...

4 Commits
v1 ... v1.0.8

Author SHA1 Message Date
renzhiyuan c0397c568e first push 2025-07-28 16:49:12 +08:00
renzhiyuan 8fe8d3f716 first push 2025-06-25 10:50:57 +08:00
renzhiyuan d437bfae2b first push 2025-06-23 20:00:49 +08:00
renzhiyuan f455983911 first push 2025-02-19 16:18:27 +08:00
2 changed files with 35 additions and 4 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module gitea.cdlsxd.cn/self-tools/l-request module gitea.cdlsxd.cn/self-tools/l_request
go 1.21 go 1.21

View File

@ -1,6 +1,7 @@
package l_request package l_request
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"io" "io"
"net/http" "net/http"
@ -21,6 +22,7 @@ type Request struct {
Files map[string]string `json:"files"` // todo 处理 Files Files map[string]string `json:"files"` // todo 处理 Files
Raw string `json:"raw"` // 原始请求数据 Raw string `json:"raw"` // 原始请求数据
JsonByte []byte `json:"json_raw"` // JSON格式请求数据 todo 多层 嵌套 JsonByte []byte `json:"json_raw"` // JSON格式请求数据 todo 多层 嵌套
Xml []byte `json:"xml"` // xml
} }
// 响应结构体 // 响应结构体
@ -68,14 +70,26 @@ func (r *Request) getData() io.Reader {
urlValues.Add(key, value) urlValues.Add(key, value)
} }
reqBody = urlValues.Encode() reqBody = urlValues.Encode()
r.Headers["Content-Type"] = "application/x-www-form-urlencoded" if _, ex := r.Headers["Content-Type"]; !ex {
r.Headers["Content-Type"] = "application/x-www-form-urlencoded"
}
} else if r.Json != nil { } else if r.Json != nil {
bytesData, _ := json.Marshal(r.Json) bytesData, _ := json.Marshal(r.Json)
reqBody = string(bytesData) reqBody = string(bytesData)
r.Headers["Content-Type"] = "application/json" if _, ex := r.Headers["Content-Type"]; !ex {
r.Headers["Content-Type"] = "application/json"
}
} else if r.JsonByte != nil { } else if r.JsonByte != nil {
reqBody = string(r.JsonByte) reqBody = string(r.JsonByte)
r.Headers["Content-Type"] = "application/json" if _, ex := r.Headers["Content-Type"]; !ex {
r.Headers["Content-Type"] = "application/json"
}
} else if r.Xml != nil {
reqBody = string(r.Xml)
if _, ex := r.Headers["Content-Type"]; !ex {
r.Headers["Content-Type"] = "application/xml"
}
} }
return strings.NewReader(reqBody) return strings.NewReader(reqBody)
} }
@ -128,3 +142,20 @@ func (r *Request) Send() (Response, error) {
elapsed := time.Since(start).Seconds() elapsed := time.Since(start).Seconds()
return r.packResponse(res, elapsed), nil return r.packResponse(res, elapsed), nil
} }
// 跳过证书发送请求
func (r *Request) SendWithoutSsl() (Response, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
req := r.prepare()
client := &http.Client{Transport: tr}
start := time.Now()
res, err := client.Do(req)
if err != nil {
return Response{}, err
}
defer res.Body.Close()
elapsed := time.Since(start).Seconds()
return r.packResponse(res, elapsed), nil
}