Skip to content

Commit

Permalink
Merge pull request #371 from reubenmiller/fix-output-template-binary-…
Browse files Browse the repository at this point in the history
…escaping

fix(outputTemplate): unescape string output
  • Loading branch information
reubenmiller committed May 18, 2024
2 parents 163a8a7 + ede685e commit f6641dc
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func (r *RequestHandler) ProcessResponse(resp *c8y.Response, respError error, in
}
}

if resp != nil && respError == nil && (r.Config.IsResponseOutput() || resp.Response.Header.Get("Content-Type") == "application/octet-stream") && len(resp.Body()) > 0 {
if resp != nil && respError == nil && !hasOutputTemplate && (r.Config.IsResponseOutput() || resp.Response.Header.Get("Content-Type") == "application/octet-stream") && len(resp.Body()) > 0 {
// estimate size based on utf8 encoding. 1 char is 1 byte
r.Logger.Debugf("Writing https response output")

Expand Down Expand Up @@ -873,11 +873,27 @@ func (r *RequestHandler) ProcessResponse(resp *c8y.Response, respError error, in
resp.SetBody(pretty.Ugly(tmplOutput))
} else {
isJSONResponse = false
// TODO: Is removing the quotes doing too much, what happens if someone is building csv, and it using quotes around some fields?
// e.g. `"my value",100`, that would get transformed to `my value",100`
// Trim any quotes wrapping the values
tmplOutput = bytes.TrimSpace(tmplOutput)
resp.SetBody(bytes.Trim(tmplOutput, "\""))
// Try to unmarshal json for situations when the user has used "response.body"
// and body contains escaped json chars (e.g. \n)
// https://github.com/reubenmiller/go-c8y-cli/issues/306
var maybeJSON any
jsonErr := json.Unmarshal(tmplOutput, &maybeJSON)
skipTrimming := false
if jsonErr == nil {
switch v := maybeJSON.(type) {
case string:
tmplOutput = []byte(v)
skipTrimming = true
}
}
if !skipTrimming {
// TODO: Is removing the quotes doing too much, what happens if someone is building csv, and it using quotes around some fields?
// e.g. `"my value",100`, that would get transformed to `my value",100`
// Trim any quotes wrapping the values
tmplOutput = bytes.TrimSpace(tmplOutput)
tmplOutput = bytes.Trim(tmplOutput, "\"")
}
resp.SetBody(tmplOutput)
}
}

Expand Down

0 comments on commit f6641dc

Please sign in to comment.