I am trying to POST a payload to a CMS using URLFetchApp. The payload has quotes in the text. I am using URLFetchApp because the code is executing inside a Google Apps Script, which from what I understand does not support libraries like request or https.
This is the payload, which matches the specifications of the CMS's API documentation (and which I have successfully POSTed to the CMS using Node libraries):
{"content":" Name Short overview "}
Then I run this code...
var data = {
"content": finalContent
}
payload = JSON.stringify(data);
...which produces the payload variable equaling this string, which is single escaped:
{"content":" Name Short overview "}
Then I plug the payload variable into the URLFetchApp code:
const url = "https://.com/api/v2/documents/1234";
const params = {
headers: {
Authorization: "Basic ",
accept: "application/json",
},
method: "put",
payload: payload,
contentType: "application/json"
};
const res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText());
But according to the logs, the payload URLFetchApp sends this string that is double escaped:
{"content":" Name Short overview "}
The CMS continually throws this error:
{"error":{"code":422,"type":"unprocessable_entity","message":"Text not allowed in DocBook element \u201clistitem\u201d in this context."}}
I assume the error message is referring to this part of the string and possibly the second backslash at the end: xmlns=\\"http://docbook.org/ns/docbook\\"
But when I send the payload through URLFetchApp without stringifying it, nothing is escaped. This is the payload sent by URLFetchApp:
{"content":" Name Short overview "}
This is the error returned by the CMS:
{"error":{"code":400,"type":"bad_request","message":"Invalid body."}}
I have tried:
- Constructing payloads with single quotes around the key and value pair
- Constructing payloads that include the
contentkey - Constructing payloads that nest the
contentkey underpayloadin the request - All kinds of combinations of single quotes, double quotes, and escaped quotes, and different kinds of encoding
All fail with one of the two errors included above.
Is it possible to send a payload through URLFetchApp with only one escape per quote?