mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
body bug
This commit is contained in:
36
body.go
36
body.go
@@ -263,21 +263,22 @@ func any2Map(val any) map[string]any {
|
||||
}
|
||||
|
||||
func (obj *RequestOption) newBody(val any, valType int) (reader io.Reader, parseOrderMap *orderMap, orderKey []string, err error) {
|
||||
switch value := val.(type) {
|
||||
case io.Reader:
|
||||
obj.once = true
|
||||
return value, nil, nil, nil
|
||||
case string:
|
||||
return bytes.NewReader(tools.StringToBytes(value)), nil, nil, nil
|
||||
case []byte:
|
||||
return bytes.NewReader(value), nil, nil, nil
|
||||
}
|
||||
if valType == readType {
|
||||
enData, err := gson.Encode(val)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
switch value := val.(type) {
|
||||
case io.Reader:
|
||||
obj.once = true
|
||||
return value, nil, nil, nil
|
||||
case string:
|
||||
return bytes.NewReader(tools.StringToBytes(value)), nil, nil, nil
|
||||
case []byte:
|
||||
return bytes.NewReader(value), nil, nil, nil
|
||||
default:
|
||||
enData, err := gson.Encode(val)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return bytes.NewReader(enData), nil, nil, nil
|
||||
}
|
||||
return bytes.NewReader(enData), nil, nil, nil
|
||||
}
|
||||
if mapData := any2Map(val); mapData != nil {
|
||||
val = mapData
|
||||
@@ -313,7 +314,14 @@ mapL:
|
||||
return nil, orderMap, nil, nil
|
||||
}
|
||||
if val, err = gson.Decode(val); err != nil {
|
||||
return nil, nil, nil, err
|
||||
switch value := val.(type) {
|
||||
case string:
|
||||
return bytes.NewReader(tools.StringToBytes(value)), nil, nil, nil
|
||||
case []byte:
|
||||
return bytes.NewReader(value), nil, nil, nil
|
||||
default:
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
}
|
||||
goto mapL
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -21,13 +20,10 @@ func (obj *RequestOption) initHeaders() (http.Header, error) {
|
||||
case http.Header:
|
||||
return headers.Clone(), nil
|
||||
default:
|
||||
body, dataMap, _, err := obj.newBody(headers, mapType)
|
||||
_, dataMap, _, err := obj.newBody(headers, mapType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
return nil, errors.New("headers type error")
|
||||
}
|
||||
if dataMap == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
24
option.go
24
option.go
@@ -1,8 +1,8 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -125,9 +125,6 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
return nil, errors.New("form type error")
|
||||
}
|
||||
if orderMap == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -154,23 +151,27 @@ func (obj *RequestOption) initBody(ctx context.Context) (io.Reader, error) {
|
||||
if orderMap == nil {
|
||||
return nil, nil
|
||||
}
|
||||
body = orderMap.parseData()
|
||||
if body == nil {
|
||||
body2 := orderMap.parseData()
|
||||
if body2 == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return body, nil
|
||||
return body2, nil
|
||||
} else if obj.Json != nil {
|
||||
body, _, _, err := obj.newBody(obj.Json, readType)
|
||||
_, orderMap, _, err := obj.newBody(obj.Json, mapType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if obj.ContentType == "" {
|
||||
obj.ContentType = "application/json"
|
||||
}
|
||||
body, err := orderMap.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return body, nil
|
||||
return bytes.NewReader(body), nil
|
||||
} else if obj.Text != nil {
|
||||
body, _, _, err := obj.newBody(obj.Text, readType)
|
||||
if err != nil {
|
||||
@@ -191,13 +192,10 @@ func (obj *RequestOption) initParams() (*url.URL, error) {
|
||||
if obj.Params == nil {
|
||||
return obj.Url, nil
|
||||
}
|
||||
body, dataMap, _, err := obj.newBody(obj.Params, mapType)
|
||||
_, dataMap, _, err := obj.newBody(obj.Params, mapType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
return nil, errors.New("params type error")
|
||||
}
|
||||
query := dataMap.parseParams().String()
|
||||
if query == "" {
|
||||
return obj.Url, nil
|
||||
|
||||
20
test/proxy/disProxy_test.go
Normal file
20
test/proxy/disProxy_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gospider007/requests"
|
||||
)
|
||||
|
||||
func TestDisProxy(t *testing.T) {
|
||||
resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Proxy: "http://192.368.7.256:9887",
|
||||
DisProxy: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Error("status code is not 200")
|
||||
}
|
||||
}
|
||||
@@ -88,3 +88,18 @@ func TestSendDataWithGson(t *testing.T) {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
func TestSendDataWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Data: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := resp.Json()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jsonData.Get("headers.Content-Type").String() != "application/x-www-form-urlencoded" {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,3 +113,15 @@ func TestSendFormWithOrderMap(t *testing.T) {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendFileWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,3 +128,15 @@ func TestSendJsonWithOrder(t *testing.T) {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendJsonWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Form: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,3 +77,15 @@ func TestSendParamsWithGson(t *testing.T) {
|
||||
t.Fatal("json data error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendParamsWithEmptiyMap(t *testing.T) {
|
||||
resp, err := requests.Post(nil, "https://httpbin.org/anything", requests.RequestOption{
|
||||
Params: map[string]string{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
t.Fatal("status code error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user