Skip to content

Commit

Permalink
Set default content-type in request modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyaw Myint Thein committed Oct 12, 2021
1 parent 3f93cee commit 71640cd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions querystring2body.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)

Expand All @@ -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})
}
Expand All @@ -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
}

0 comments on commit 71640cd

Please sign in to comment.