SetBody set the request Body, accepts string, []byte, io.Reader, map and struct.
(body any)
| 863 | |
| 864 | // SetBody set the request Body, accepts string, []byte, io.Reader, map and struct. |
| 865 | func (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. |
| 901 | func (r *Request) SetBodyBytes(body []byte) *Request { |