I'm writing a program that sends a POST request whose body requires double quotes in plaintext. Because I'm injecting variables with fmt.Sprintf everytime I send a request, I'm using string literals when sending the request off
func searchLocalImage(imageName, imageTag, artifactoryUsername, artifactoryPassword string) (localImageRepo string) {
a := types.ArtifactoryClient{
Url: "<url>",
Username: artifactoryUsername,
Password: artifactoryPassword,
Client: resty.New(),
}
var responseBody map[string]interface{}
url := a.Url + "/search/aql"
res, _ := a.Client.R().
SetHeader("Content-Type", "text/plain").
SetBasicAuth(a.Username, a.Password).
SetBody(fmt.Sprintf(`items.find({"$and": [{"repo":{"$match":"<folder>"}},{"path": {"$match":"%s/%s"}},{"name":{"$match":"<tag>"}}]}).include("repo","path","name")`, imageName, imageTag)).
Post(url)
Of course, this automatically injects escape characters to returned string.
"items.find({\"$and\": [{\"repo\":{\"$match\":\"<folder>\"}},{\"path\": {\"$match\":\"<folderPath>\"}},{\"name\":{\"$match\":\"<tag>\"}}]}).include(\"repo\",\"path\",\"name\")"
This unfortunately results in the response being an error message. Has anybody found a way to send a request like this the way I would need to without also sending those backslashes?
No, this is not true. You see the escape characters because the tool that you used (most likely a debugger) displays the content as a quoted string.
You can simply dump the request to see what is sent. And paste the dump content into Insomnia to find out what's wrong.