From 0fc7fb26678ac9749f382a62bb23f2d62c20bf83 Mon Sep 17 00:00:00 2001 From: oleksii khliupin Date: Thu, 9 Feb 2017 17:01:07 -0500 Subject: [PATCH] Add EncodeXMLRequest --- transport/http/client.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/transport/http/client.go b/transport/http/client.go index b53368700..494797583 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "encoding/xml" "io/ioutil" "net/http" "net/url" @@ -132,3 +133,18 @@ func EncodeJSONRequest(c context.Context, r *http.Request, request interface{}) r.Body = ioutil.NopCloser(&b) return json.NewEncoder(&b).Encode(request) } + +// EncodeXMLRequest is an EncodeRequestFunc that serializes the request as a +// XML object to the Request body. If the request implements Headerer, +// the provided headers will be applied to the request. +func EncodeXMLRequest(c context.Context, r *http.Request, request interface{}) error { + r.Header.Set("Content-Type", "text/xml; charset=utf-8") + if headerer, ok := request.(Headerer); ok { + for k := range headerer.Headers() { + r.Header.Set(k, headerer.Headers().Get(k)) + } + } + var b bytes.Buffer + r.Body = ioutil.NopCloser(&b) + return xml.NewEncoder(&b).Encode(request) +}