@@ -19,6 +19,8 @@ import (
1919 bytes "bytes"
2020 http "net/http"
2121 strings "strings"
22+ url "net/url"
23+ multipart "mime/multipart"
2224)
2325// Reference imports to suppress errors if they are not otherwise used.
2426var _ = context.Background
@@ -28,6 +30,8 @@ var _ = bytes.Compare
2830var _ = json.Marshal
2931var _ = strings.Compare
3032var _ = fmt.Errorf
33+ var _ = url.Parse
34+ var _ = multipart.ErrMessageTooLarge
3135
3236{{ range .Services }}
3337// Client API for {{ .ServName }} service
@@ -68,11 +72,14 @@ var requestCode = `// options
6872 opt := buildOptions(c.opts, opts...)
6973 headers := make(map[string]string)
7074 // route
71- {{ .RouteCode | html }}
75+ {{ .RouteCode }}
7276 // body
73- var body io.Reader
74- {{ .BodyCode | html }}
77+ {{ .BodyCode }}
78+ {{- if eq .BodyCode "" -}}
79+ req, err := http.NewRequest("{{ .Verb }}", rawURL, nil)
80+ {{- else }}
7581 req, err := http.NewRequest("{{ .Verb }}", rawURL, body)
82+ {{- end }}
7683 if err != nil {
7784 return nil, err
7885 }
@@ -93,31 +100,24 @@ var requestCode = `// options
93100 return &res, err
94101`
95102
96- var bodyFormCode = `bodyForms := make(map[string]string)
97- {{ .Body | html }}
98- var bs []string
99- for k, v := range bodyForms {
100- bs = append(bs, fmt.Sprintf("%s=%s", k, v))
101- }
102- body = strings.NewReader(strings.Join(bs, "&"))
103+ var bodyFormCode = `bodyForms := url.Values{}
104+ {{ .Body }}
105+ body := strings.NewReader(bodyForms.Encode())
103106 headers["Content-Type"] = "application/x-www-form-urlencoded"
104107`
105108
106- var bodyMultiCode = `bodyForms := make(map[string]string)
107- {{ .Body | html }}
108- var bs []string
109- for k, v := range bodyForms {
110- bs = append(bs, fmt.Sprintf("%s=%s", k, v))
111- }
112- body = strings.NewReader(strings.Join(bs, "&"))
109+ var bodyMultiCode = `body := new(bytes.Buffer)
110+ bodyForms := multipart.NewWriter(body)
111+ {{ .Body }}
112+ defer func() { _ = bodyForms.Close() } ()
113113 headers["Content-Type"] = "multipart/form-data"
114114`
115115
116116var bodyJsonCode = `bs, err := json.Marshal({{ .Body | html }})
117117 if err != nil {
118118 return nil, err
119119 }
120- body = bytes.NewReader(bs)
120+ body : = bytes.NewReader(bs)
121121 headers["Content-Type"] = "application/json"
122122`
123123
0 commit comments