MCPcopy Index your code
hub / github.com/imroc/req / SetBody

Method SetBody

request.go:865–898  ·  view source on GitHub ↗

SetBody set the request Body, accepts string, []byte, io.Reader, map and struct.

(body any)

Source from the content-addressed store, hash-verified

863
864// SetBody set the request Body, accepts string, []byte, io.Reader, map and struct.
865func (r *Request) SetBody(body any) *Request {
866 if body == nil {
867 return r
868 }
869 switch b := body.(type) {
870 case io.ReadCloser:
871 r.unReplayableBody = b
872 r.GetBody = func() (io.ReadCloser, error) {
873 return r.unReplayableBody, nil
874 }
875 case io.Reader:
876 r.unReplayableBody = io.NopCloser(b)
877 r.GetBody = func() (io.ReadCloser, error) {
878 return r.unReplayableBody, nil
879 }
880 case []byte:
881 r.SetBodyBytes(b)
882 case string:
883 r.SetBodyString(b)
884 case func() (io.ReadCloser, error):
885 r.GetBody = b
886 case GetContentFunc:
887 r.GetBody = b
888 default:
889 t := reflect.TypeOf(body)
890 switch t.Kind() {
891 case reflect.Ptr, reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
892 r.marshalBody = body
893 default:
894 r.SetBodyString(fmt.Sprint(body))
895 }
896 }
897 return r
898}
899
900// SetBodyBytes set the request Body as []byte.
901func (r *Request) SetBodyBytes(body []byte) *Request {

Callers 9

testEnableDumpAllFunction · 0.45
TestEnableDumpAllToFileFunction · 0.45
SetBodyFunction · 0.45
testEnableDumpFunction · 0.45
TestSetBodyMarshalFunction · 0.45
TestSetBodyFunction · 0.45

Calls 2

SetBodyBytesMethod · 0.95
SetBodyStringMethod · 0.95

Tested by 8

testEnableDumpAllFunction · 0.36
TestEnableDumpAllToFileFunction · 0.36
testEnableDumpFunction · 0.36
TestSetBodyMarshalFunction · 0.36
TestSetBodyFunction · 0.36