Package list golang-github-go-kit-kit / 0fc7fb2
Add EncodeXMLRequest oleksii khliupin 4 years ago
1 changed file(s) with 16 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
33 "bytes"
44 "context"
55 "encoding/json"
6 "encoding/xml"
67 "io/ioutil"
78 "net/http"
89 "net/url"
131132 r.Body = ioutil.NopCloser(&b)
132133 return json.NewEncoder(&b).Encode(request)
133134 }
135
136 // EncodeXMLRequest is an EncodeRequestFunc that serializes the request as a
137 // XML object to the Request body. If the request implements Headerer,
138 // the provided headers will be applied to the request.
139 func EncodeXMLRequest(c context.Context, r *http.Request, request interface{}) error {
140 r.Header.Set("Content-Type", "text/xml; charset=utf-8")
141 if headerer, ok := request.(Headerer); ok {
142 for k := range headerer.Headers() {
143 r.Header.Set(k, headerer.Headers().Get(k))
144 }
145 }
146 var b bytes.Buffer
147 r.Body = ioutil.NopCloser(&b)
148 return xml.NewEncoder(&b).Encode(request)
149 }