调整请求

This commit is contained in:
Yun
2025-09-13 22:20:06 +08:00
parent 14bb42c131
commit 6a9f388bb6
6 changed files with 189 additions and 255 deletions
+18 -43
View File
@@ -135,26 +135,6 @@ func (c *Curlx) Send(ctx context.Context, p ...Param) (res []byte, httpcode int,
status := response.StatusCode // 获取状态码,正常是200
var body []byte
// switch response.Header.Get("Content-Encoding") {
// case "gzip":
// reader, err := gzip.NewReader(response.Body)
// if err != nil {
// return nil, status, err
// }
// for {
// buf := make([]byte, 1024)
// n, err := reader.Read(buf)
// if err != nil && err != io.EOF {
// panic(err)
// }
// if n == 0 {
// break
// }
// body = append(body, buf...)
// }
// default:
// body, _ = io.ReadAll(response.Body)
// }
if response.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(response.Body)
@@ -177,6 +157,24 @@ func (c *Curlx) Send(ctx context.Context, p ...Param) (res []byte, httpcode int,
return body, status, nil
}
// PostJson 发送JSON数据
func (l *Curlx) PostJson(ctx context.Context, url string, jsonStr string) ([]byte, int, error) {
return l.Send(ctx,
SetParamsUrl(url),
SetParamsBody([]byte(jsonStr)),
SetParamsContentType(ContentTypeJson),
SetParamsMethod(MethodPost),
)
}
// Get 简单GET请求
func (l *Curlx) Get(ctx context.Context, url string) ([]byte, int, error) {
return l.Send(ctx,
SetParamsUrl(url),
SetParamsMethod(MethodGet),
)
}
func (c *Curlx) SendWithResponee(ctx context.Context, ps ...Param) Response {
r := Response{}
req, resp, err := c.SendExec(ctx, ps...)
@@ -188,29 +186,6 @@ func (c *Curlx) SendWithResponee(ctx context.Context, ps ...Param) Response {
return r
}
var body []byte
// switch resp.Header.Get("Content-Encoding") {
// case "gzip":
// reader, err := gzip.NewReader(resp.Body)
// if err != nil {
// r.err = err
// return r
// }
// for {
// buf := make([]byte, 1024)
// n, err := reader.Read(buf)
// if err != nil && err != io.EOF {
// panic(err)
// }
// if n == 0 {
// break
// }
// // 读取n个字节
// body = append(body, buf[:n]...)
// // body = append(body, buf...)
// }
// default:
// body, _ = io.ReadAll(resp.Body)
// }
if resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(resp.Body)
+8 -3
View File
@@ -2,8 +2,10 @@ package curlx
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"testing"
)
@@ -36,12 +38,15 @@ func TestForm(t *testing.T) {
},
}
by, _ := json.Marshal(s)
p := ClientParams{
Url: "http://tech-dev.sealmoo.com/api/material/upload",
Method: "POST",
Body: s,
Headers: map[string]interface{}{
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8",
Body: by,
Headers: http.Header{
"Content-Type": []string{"application/json"},
"Authorization": []string{"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8"},
},
ContentType: ContentTypeForm,
}
+1
View File
@@ -3,6 +3,7 @@ module github.com/yuninks/curlx
go 1.20
require (
code.yun.ink/pkg/convx v1.0.2
github.com/tidwall/gjson v1.17.0
golang.org/x/net v0.18.0
)
+2
View File
@@ -1,3 +1,5 @@
code.yun.ink/pkg/convx v1.0.2 h1:vkEcDQ8w9Kz2T/RMnYefkzyXQwqI9nnaWo+Z1jlS7IE=
code.yun.ink/pkg/convx v1.0.2/go.mod h1:6xqmUend1kwarRvJ0TQlfzzS4QCWdRrXQiUY/ggzYqo=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
+78 -18
View File
@@ -1,17 +1,22 @@
package curlx
import (
"encoding/json"
"net/http"
)
type ClientParams struct {
Url string
Method Method // GET/POST
Body interface{}
Headers map[string]interface{}
Cookies interface{}
Method Method // GET/POST/PUT/DELETE
Body []byte
Headers http.Header
Cookies []http.Cookie
ContentType ContentType // FORM,JSON,XML
}
func defaultParams() ClientParams {
return ClientParams{
Headers: map[string]interface{}{}, // 初始化map
Headers: http.Header{},
}
}
@@ -49,9 +54,22 @@ func SetParamsMethod(m Method) Param {
/**
* 设置参数
*/
func SetParamsBody(p interface{}) Param {
func SetParamsBody(by []byte) Param {
return func(param *ClientParams) {
param.Body = p
param.Body = by
}
}
func SetParamsBodyAny(v interface{}) Param {
return func(param *ClientParams) {
switch value := v.(type) {
case []byte:
param.Body = value
case string:
param.Body = []byte(value)
default:
param.Body, _ = json.Marshal(value)
}
}
}
@@ -60,12 +78,18 @@ func SetParamsBody(p interface{}) Param {
*/
func SetParamsFormText(fieldName, fieldValue string) Param {
return func(param *ClientParams) {
fp := param.Body.([]FormParam)
fp = append(fp, FormParam{
m := []FormParam{}
if param.Body != nil {
json.Unmarshal(param.Body, &m)
}
m = append(m, FormParam{
FieldName: fieldName,
FieldValue: fieldValue,
FieldType: FieldTypeText,
})
fp, _ := json.Marshal(m)
param.Body = fp
}
}
@@ -75,24 +99,36 @@ func SetParamsFormText(fieldName, fieldValue string) Param {
*/
func SetParamsFormFile(fieldName, fileName string, fileBytes []byte) Param {
return func(param *ClientParams) {
fp := param.Body.([]FormParam)
fp := []FormParam{}
if param.Body != nil {
json.Unmarshal(param.Body, &fp)
}
fp = append(fp, FormParam{
FieldName: fieldName,
FieldType: FieldTypeFile,
FileName: fileName,
FileBytes: fileBytes,
})
param.Body = fp
fpb, _ := json.Marshal(fp)
param.Body = fpb
}
}
/**
* 设置请求头
*/
func SetParamsHeaders(h map[string]interface{}) Param {
func SetParamsHeaders(h map[string]string) Param {
return func(param *ClientParams) {
for key, _ := range h {
param.Headers[key] = h[key]
if param.Headers == nil {
param.Headers = http.Header{}
}
for k, v := range h {
param.Headers.Set(k, v)
}
}
}
@@ -102,7 +138,10 @@ func SetParamsHeaders(h map[string]interface{}) Param {
*/
func SetParamsHeader(key, value string) Param {
return func(param *ClientParams) {
param.Headers[key] = value
if param.Headers == nil {
param.Headers = http.Header{}
}
param.Headers.Add(key, value)
}
}
@@ -111,7 +150,25 @@ func SetParamsHeader(key, value string) Param {
*/
func SetUserAgent(userAgent UserAgent) Param {
return func(param *ClientParams) {
param.Headers["User-Agent"] = string(userAgent)
if param.Headers == nil {
param.Headers = http.Header{}
}
param.Headers.Set("User-Agent", string(userAgent))
}
}
func SetCookie(name, value string) Param {
return func(param *ClientParams) {
param.Cookies = append(param.Cookies, http.Cookie{
Name: name,
Value: value,
})
}
}
func SetCookies(cookies []http.Cookie) Param {
return func(param *ClientParams) {
param.Cookies = append(param.Cookies, cookies...)
}
}
@@ -120,14 +177,17 @@ func SetUserAgent(userAgent UserAgent) Param {
*/
func SetReferer(referer string) Param {
return func(param *ClientParams) {
param.Headers["Referer"] = referer
if param.Headers == nil {
param.Headers = http.Header{}
}
param.Headers.Set("Referer", referer)
}
}
/**
* 设置cookies
*/
func SetParamsCookies(c interface{}) Param {
func SetParamsCookies(c []http.Cookie) Param {
return func(param *ClientParams) {
param.Cookies = c
}
+60 -169
View File
@@ -3,14 +3,14 @@ package curlx
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"code.yun.ink/pkg/convx"
)
/**
@@ -38,25 +38,13 @@ func (p *ClientParams) parseUrl() error {
* 处理请求头Header
*/
func (p *ClientParams) parseHeaders(r *http.Request) {
if p.Headers != nil {
if r.Header.Get("User-Agent") == "" {
r.Header.Add("User-Agent", string(UserAgentChrome))
}
for k, v := range p.Headers {
switch value := v.(type) {
case string:
r.Header.Set(k, value)
case []string:
for _, vv := range value {
r.Header.Add(k, vv)
}
case ContentType:
r.Header.Set(k, string(value))
case UserAgent:
r.Header.Set(k, string(value))
}
}
if p.Headers.Get("User-Agent") == "" {
p.Headers.Add("User-Agent", string(UserAgentChrome))
}
r.Header = p.Headers
}
/**
@@ -67,35 +55,29 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
// 初始化(如未初始化)
if p.Headers == nil {
p.Headers = make(map[string]interface{})
p.Headers = http.Header{}
}
if p.Body != nil {
if p.ContentType == ContentTypeJson {
// 判断是否存在
// 添加Content-Type
if _, ok := p.Headers["Content-Type"]; !ok {
p.Headers["Content-Type"] = ContentTypeJson
}
strParam, ok := p.Body.(string)
if ok {
return bytes.NewReader([]byte(strParam)), nil
}
b, err := json.Marshal(p.Body)
if err == nil {
return bytes.NewReader(b), nil
}
} else if p.ContentType == ContentTypeForm {
// 表单上传(可能有文件)
// 文件上传的
params := []FormParam{}
if value, ok := p.Body.([]FormParam); ok {
params = value
} else if value, ok := p.Body.(FormParam); ok {
params = append(params, value)
} else {
return nil, errors.New("表单上传的参数格式不正确")
p.Headers.Set("Content-Type", string(p.ContentType))
}
if len(p.Body) == 0 {
return nil, nil
}
switch p.ContentType {
case ContentTypeJson:
// JSON
return bytes.NewReader(p.Body), nil
case ContentTypeForm:
// 表单
params := []FormParam{}
err = json.Unmarshal(p.Body, &params)
if err != nil {
return nil, err
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for _, v := range params {
@@ -107,126 +89,50 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
}
}
writer.Close()
p.Headers["Content-Type"] = writer.FormDataContentType()
p.Headers.Set("Content-Type", writer.FormDataContentType())
return body, nil
} else if p.ContentType == ContentTypeXml {
if _, ok := p.Headers["Content-Type"]; !ok {
p.Headers["Content-Type"] = ContentTypeXml
case ContentTypeXml:
// XML
return bytes.NewReader(p.Body), nil
case ContentTypeText:
// TEXT
return bytes.NewReader(p.Body), nil
case ContentTypeUrlEncoded:
// URL编码
m := map[string]any{}
if err = json.Unmarshal(p.Body, &m); err != nil {
return nil, err
}
var string_data string
if value, ok := p.Body.(string); ok {
string_data = string(value)
} else {
var by []byte
by, err = xml.Marshal(p.Body)
if err != nil {
return
}
string_data = string(by)
}
return strings.NewReader(string_data), nil
} else if p.ContentType == ContentTypeText {
if _, ok := p.Headers["Content-Type"]; !ok {
p.Headers["Content-Type"] = ContentTypeText
}
var string_data string
if value, ok := p.Body.(string); ok {
string_data = string(value)
} else {
err = errors.New("TEXT类型的参数仅支持字符串")
return
}
return strings.NewReader(string_data), nil
} else if p.ContentType == ContentTypeUrlEncoded {
if _, ok := p.Headers["Content-Type"]; !ok {
p.Headers["Content-Type"] = "application/x-www-form-urlencoded"
}
// 判断需要map[string]interface{}类型
paramValue, ok := p.Body.(map[string]interface{})
if !ok {
return strings.NewReader(""), errors.New("参数需map[string]interface{}")
}
values := url.Values{}
for k, v := range paramValue {
// 字符串
if v_string, ok := v.(string); ok {
values.Set(k, v_string)
}
// 字符串切片
if vv, ok := v.([]string); ok {
for _, vvv := range vv {
values.Add(k+"[]", vvv)
}
}
// int转string
if v_int, ok := v.(int); ok {
values.Set(k, strconv.Itoa(v_int))
}
// int64转string
if v_int64, ok := v.(int64); ok {
values.Set(k, strconv.FormatInt(v_int64, 10))
}
// float32转string
if v_float32, ok := v.(float32); ok {
values.Set(k, strconv.FormatFloat(float64(v_float32), 'f', -1, 32))
}
// float64转string
if v_float64, ok := v.(float64); ok {
values.Set(k, strconv.FormatFloat(v_float64, 'f', -1, 64))
}
for k, v := range m {
val, _ := convx.ToString(v)
values.Set(k, val)
}
return strings.NewReader(values.Encode()), nil
} else {
// 如果是GET请求
default:
if p.Method == MethodGet {
// 判断需要map[string]interface{}类型
paramValue, ok := p.Body.(map[string]interface{})
if !ok {
return strings.NewReader(""), errors.New("参数需map[string]interface{}")
m := map[string]any{}
if err = json.Unmarshal(p.Body, &m); err != nil {
return nil, err
}
// 拼接参数到URL
if strings.Contains(p.Url, "?") {
p.Url += "&"
} else {
p.Url += "?"
}
for k, v := range paramValue {
// 字符串
if v_string, ok := v.(string); ok {
p.Url += k + "=" + v_string + "&"
}
// 字符串切片
if vv, ok := v.([]string); ok {
for _, vvv := range vv {
p.Url += k + "[]=" + vvv + "&"
}
}
// int转string
if v_int, ok := v.(int); ok {
p.Url += k + "=" + strconv.Itoa(v_int) + "&"
}
// int64转string
if v_int64, ok := v.(int64); ok {
p.Url += k + "=" + strconv.FormatInt(v_int64, 10) + "&"
}
// float32转string
if v_float32, ok := v.(float32); ok {
p.Url += k + "=" + strconv.FormatFloat(float64(v_float32), 'f', -1, 32) + "&"
}
// float64转string
if v_float64, ok := v.(float64); ok {
p.Url += k + "=" + strconv.FormatFloat(v_float64, 'f', -1, 64) + "&"
url, err := url.Parse(p.Url) // 解析URL
if err != nil {
return nil, err
}
query := url.Query()
for k, v := range m {
val, _ := convx.ToString(v)
query[k] = append(query[k], val)
}
url.RawQuery = query.Encode()
p.Url = url.String()
} else {
return nil, errors.New("curlx 不支持的数据类型")
}
}
}
return
@@ -236,23 +142,8 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
* 处理Cookie
*/
func (p *ClientParams) parseCookies(r *http.Request) {
switch p.Cookies.(type) {
case string:
cookies := p.Cookies.(string)
r.Header.Add("Cookie", cookies)
case map[string]string:
cookies := p.Cookies.(map[string]string)
for k, v := range cookies {
r.AddCookie(&http.Cookie{
Name: k,
Value: v,
})
}
case []*http.Cookie:
cookies := p.Cookies.([]*http.Cookie)
for _, cookie := range cookies {
r.AddCookie(cookie)
}
for _, cookie := range p.Cookies {
r.AddCookie(&cookie)
}
}