Skip to content

Commit

Permalink
Merge pull request #456 from gaplyk/xml-encoder
Browse files Browse the repository at this point in the history
Add EncodeXMLRequest
  • Loading branch information
peterbourgon authored Feb 10, 2017
2 parents e3613f6 + 0fc7fb2 commit 704043a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions transport/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -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)
}

0 comments on commit 704043a

Please sign in to comment.