diff --git a/querystring2body.go b/querystring2body.go index 18b5375..17669f8 100644 --- a/querystring2body.go +++ b/querystring2body.go @@ -10,14 +10,21 @@ import ( "github.com/google/martian/parse" ) +const ( + _contentType_applicationJSON = "appliction/json" + _contentTypeHeader = "Content-Type" +) + type ( QueryModifierConfig struct { KeysToExtract []string `json:"keys_to_extract"` Template string `json:"template"` + ContentType string `json:"content_type"` } Query2BodyModifier struct { keysToExtract []string template *template.Template + ContentType string } ) @@ -35,6 +42,7 @@ func queryModifierFromJSON(b []byte) (*parse.Result, error) { mod := &Query2BodyModifier{ keysToExtract: cfg.KeysToExtract, template: tmpl, + ContentType: cfg.ContentType, } return parse.NewResult(mod, []parse.ModifierType{parse.Request}) } @@ -54,5 +62,15 @@ func (m *Query2BodyModifier) ModifyRequest(req *http.Request) error { req.ContentLength = int64(buf.Len()) req.Body = ioutil.NopCloser(buf) req.URL.RawQuery = query.Encode() + + if m.ContentType == "" && req.Header.Get("Content-Type") == "" { + // set default content-type header as application/json + req.Header.Set(_contentTypeHeader, _contentType_applicationJSON) + } + + if m.ContentType != "" { + req.Header.Del(_contentTypeHeader) + req.Header.Set(_contentTypeHeader, m.ContentType) + } return nil }