调整请求
This commit is contained in:
@@ -135,26 +135,6 @@ func (c *Curlx) Send(ctx context.Context, p ...Param) (res []byte, httpcode int,
|
|||||||
status := response.StatusCode // 获取状态码,正常是200
|
status := response.StatusCode // 获取状态码,正常是200
|
||||||
|
|
||||||
var body []byte
|
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" {
|
if response.Header.Get("Content-Encoding") == "gzip" {
|
||||||
reader, err := gzip.NewReader(response.Body)
|
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
|
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 {
|
func (c *Curlx) SendWithResponee(ctx context.Context, ps ...Param) Response {
|
||||||
r := Response{}
|
r := Response{}
|
||||||
req, resp, err := c.SendExec(ctx, ps...)
|
req, resp, err := c.SendExec(ctx, ps...)
|
||||||
@@ -188,29 +186,6 @@ func (c *Curlx) SendWithResponee(ctx context.Context, ps ...Param) Response {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
var body []byte
|
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" {
|
if resp.Header.Get("Content-Encoding") == "gzip" {
|
||||||
reader, err := gzip.NewReader(resp.Body)
|
reader, err := gzip.NewReader(resp.Body)
|
||||||
|
|||||||
+9
-4
@@ -2,8 +2,10 @@ package curlx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -36,12 +38,15 @@ func TestForm(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
by, _ := json.Marshal(s)
|
||||||
|
|
||||||
p := ClientParams{
|
p := ClientParams{
|
||||||
Url: "http://tech-dev.sealmoo.com/api/material/upload",
|
Url: "http://tech-dev.sealmoo.com/api/material/upload",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: s,
|
Body: by,
|
||||||
Headers: map[string]interface{}{
|
Headers: http.Header{
|
||||||
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8",
|
"Content-Type": []string{"application/json"},
|
||||||
|
"Authorization": []string{"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfaWQiOjAsImNsaWVudF9pZCI6MCwidXNlcl9pZCI6MSwiZXhwIjoxNzAxMzk3NzkxfQ.9_uJ6y8I4JZTwgSenwHC_01nddLuI4zmgpyPhn5M6j8"},
|
||||||
},
|
},
|
||||||
ContentType: ContentTypeForm,
|
ContentType: ContentTypeForm,
|
||||||
}
|
}
|
||||||
@@ -52,6 +57,6 @@ func TestForm(t *testing.T) {
|
|||||||
func TestProxy(t *testing.T) {
|
func TestProxy(t *testing.T) {
|
||||||
c := NewCurlx()
|
c := NewCurlx()
|
||||||
c.WithProxySocks5("127.0.0.1:1080")
|
c.WithProxySocks5("127.0.0.1:1080")
|
||||||
res, code, err := c.Send(context.Background(), SetParamsUrl("https://www.google.com"),SetParamsMethod(MethodGet))
|
res, code, err := c.Send(context.Background(), SetParamsUrl("https://www.google.com"), SetParamsMethod(MethodGet))
|
||||||
t.Log(string(res), code, err)
|
t.Log(string(res), code, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ module github.com/yuninks/curlx
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
code.yun.ink/pkg/convx v1.0.2
|
||||||
github.com/tidwall/gjson v1.17.0
|
github.com/tidwall/gjson v1.17.0
|
||||||
golang.org/x/net v0.18.0
|
golang.org/x/net v0.18.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
|
||||||
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
|||||||
@@ -1,17 +1,22 @@
|
|||||||
package curlx
|
package curlx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type ClientParams struct {
|
type ClientParams struct {
|
||||||
Url string
|
Url string
|
||||||
Method Method // GET/POST
|
Method Method // GET/POST/PUT/DELETE
|
||||||
Body interface{}
|
Body []byte
|
||||||
Headers map[string]interface{}
|
Headers http.Header
|
||||||
Cookies interface{}
|
Cookies []http.Cookie
|
||||||
ContentType ContentType // FORM,JSON,XML
|
ContentType ContentType // FORM,JSON,XML
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultParams() ClientParams {
|
func defaultParams() ClientParams {
|
||||||
return 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) {
|
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 {
|
func SetParamsFormText(fieldName, fieldValue string) Param {
|
||||||
return func(param *ClientParams) {
|
return func(param *ClientParams) {
|
||||||
fp := param.Body.([]FormParam)
|
m := []FormParam{}
|
||||||
fp = append(fp, FormParam{
|
if param.Body != nil {
|
||||||
|
json.Unmarshal(param.Body, &m)
|
||||||
|
}
|
||||||
|
m = append(m, FormParam{
|
||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
FieldValue: fieldValue,
|
FieldValue: fieldValue,
|
||||||
FieldType: FieldTypeText,
|
FieldType: FieldTypeText,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
fp, _ := json.Marshal(m)
|
||||||
|
|
||||||
param.Body = fp
|
param.Body = fp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,24 +99,36 @@ func SetParamsFormText(fieldName, fieldValue string) Param {
|
|||||||
*/
|
*/
|
||||||
func SetParamsFormFile(fieldName, fileName string, fileBytes []byte) Param {
|
func SetParamsFormFile(fieldName, fileName string, fileBytes []byte) Param {
|
||||||
return func(param *ClientParams) {
|
return func(param *ClientParams) {
|
||||||
fp := param.Body.([]FormParam)
|
|
||||||
|
fp := []FormParam{}
|
||||||
|
|
||||||
|
if param.Body != nil {
|
||||||
|
json.Unmarshal(param.Body, &fp)
|
||||||
|
}
|
||||||
|
|
||||||
fp = append(fp, FormParam{
|
fp = append(fp, FormParam{
|
||||||
FieldName: fieldName,
|
FieldName: fieldName,
|
||||||
FieldType: FieldTypeFile,
|
FieldType: FieldTypeFile,
|
||||||
FileName: fileName,
|
FileName: fileName,
|
||||||
FileBytes: fileBytes,
|
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) {
|
return func(param *ClientParams) {
|
||||||
for key, _ := range h {
|
if param.Headers == nil {
|
||||||
param.Headers[key] = h[key]
|
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 {
|
func SetParamsHeader(key, value string) Param {
|
||||||
return func(param *ClientParams) {
|
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 {
|
func SetUserAgent(userAgent UserAgent) Param {
|
||||||
return func(param *ClientParams) {
|
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 {
|
func SetReferer(referer string) Param {
|
||||||
return func(param *ClientParams) {
|
return func(param *ClientParams) {
|
||||||
param.Headers["Referer"] = referer
|
if param.Headers == nil {
|
||||||
|
param.Headers = http.Header{}
|
||||||
|
}
|
||||||
|
param.Headers.Set("Referer", referer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置cookies
|
* 设置cookies
|
||||||
*/
|
*/
|
||||||
func SetParamsCookies(c interface{}) Param {
|
func SetParamsCookies(c []http.Cookie) Param {
|
||||||
return func(param *ClientParams) {
|
return func(param *ClientParams) {
|
||||||
param.Cookies = c
|
param.Cookies = c
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-169
@@ -3,14 +3,14 @@ package curlx
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"code.yun.ink/pkg/convx"
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,25 +38,13 @@ func (p *ClientParams) parseUrl() error {
|
|||||||
* 处理请求头Header
|
* 处理请求头Header
|
||||||
*/
|
*/
|
||||||
func (p *ClientParams) parseHeaders(r *http.Request) {
|
func (p *ClientParams) parseHeaders(r *http.Request) {
|
||||||
if p.Headers != nil {
|
|
||||||
if r.Header.Get("User-Agent") == "" {
|
if p.Headers.Get("User-Agent") == "" {
|
||||||
r.Header.Add("User-Agent", string(UserAgentChrome))
|
p.Headers.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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.Header = p.Headers
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,35 +55,29 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
|||||||
|
|
||||||
// 初始化(如未初始化)
|
// 初始化(如未初始化)
|
||||||
if p.Headers == nil {
|
if p.Headers == nil {
|
||||||
p.Headers = make(map[string]interface{})
|
p.Headers = http.Header{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Body != nil {
|
// 添加Content-Type
|
||||||
if p.ContentType == ContentTypeJson {
|
|
||||||
// 判断是否存在
|
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
if _, ok := p.Headers["Content-Type"]; !ok {
|
||||||
p.Headers["Content-Type"] = ContentTypeJson
|
p.Headers.Set("Content-Type", string(p.ContentType))
|
||||||
}
|
|
||||||
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("表单上传的参数格式不正确")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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, ¶ms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
body := &bytes.Buffer{}
|
body := &bytes.Buffer{}
|
||||||
writer := multipart.NewWriter(body)
|
writer := multipart.NewWriter(body)
|
||||||
for _, v := range params {
|
for _, v := range params {
|
||||||
@@ -107,126 +89,50 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
writer.Close()
|
writer.Close()
|
||||||
p.Headers["Content-Type"] = writer.FormDataContentType()
|
p.Headers.Set("Content-Type", writer.FormDataContentType())
|
||||||
return body, nil
|
return body, nil
|
||||||
|
case ContentTypeXml:
|
||||||
} else if p.ContentType == ContentTypeXml {
|
// XML
|
||||||
if _, ok := p.Headers["Content-Type"]; !ok {
|
return bytes.NewReader(p.Body), nil
|
||||||
p.Headers["Content-Type"] = ContentTypeXml
|
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{}
|
values := url.Values{}
|
||||||
for k, v := range paramValue {
|
for k, v := range m {
|
||||||
// 字符串
|
val, _ := convx.ToString(v)
|
||||||
if v_string, ok := v.(string); ok {
|
values.Set(k, val)
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.NewReader(values.Encode()), nil
|
return strings.NewReader(values.Encode()), nil
|
||||||
} else {
|
default:
|
||||||
// 如果是GET请求
|
|
||||||
if p.Method == MethodGet {
|
if p.Method == MethodGet {
|
||||||
// 判断需要map[string]interface{}类型
|
|
||||||
paramValue, ok := p.Body.(map[string]interface{})
|
m := map[string]any{}
|
||||||
if !ok {
|
if err = json.Unmarshal(p.Body, &m); err != nil {
|
||||||
return strings.NewReader(""), errors.New("参数需map[string]interface{}")
|
return nil, err
|
||||||
}
|
}
|
||||||
// 拼接参数到URL
|
|
||||||
if strings.Contains(p.Url, "?") {
|
url, err := url.Parse(p.Url) // 解析URL
|
||||||
p.Url += "&"
|
if err != nil {
|
||||||
} else {
|
return nil, err
|
||||||
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) + "&"
|
|
||||||
}
|
}
|
||||||
|
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 {
|
} else {
|
||||||
return nil, errors.New("curlx 不支持的数据类型")
|
return nil, errors.New("curlx 不支持的数据类型")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -236,23 +142,8 @@ func (p *ClientParams) parseParams() (str io.Reader, err error) {
|
|||||||
* 处理Cookie
|
* 处理Cookie
|
||||||
*/
|
*/
|
||||||
func (p *ClientParams) parseCookies(r *http.Request) {
|
func (p *ClientParams) parseCookies(r *http.Request) {
|
||||||
switch p.Cookies.(type) {
|
for _, cookie := range p.Cookies {
|
||||||
case string:
|
r.AddCookie(&cookie)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user