hcdocs
hcdocs

Reputation: 1309

How to properly escape payloads sent with URLFetchApp when the payload contains double quotes

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":"<?xml version="1.0"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0" xml:id="12345" version="5.0" xml:lang="en" xinfo:resource="12345" xinfo:resource-id="1234" xinfo:resource-type="component" xinfo:resource-title="Source 5" xinfo:resource-titlelabel="" xinfo:version-major="1" xinfo:version-minor="0">  <title xinfo:text="1234">Name</title>  <para>Short overview</para></section>"}

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":"<?xml version=\"1.0\"?><section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xinfo=\"http://ns.expertinfo.se/cms/xmlns/1.0\" xml:id=\"12345\" version=\"5.0\" xml:lang=\"en\" xinfo:resource=\"12345\" xinfo:resource-id=\"1234\" xinfo:resource-type=\"component\" xinfo:resource-title=\"Source 5\" xinfo:resource-titlelabel=\"\" xinfo:version-major=\"1\" xinfo:version-minor=\"0\">  <title xinfo:text=\"1234\">Name</title>  <para>Short overview</para></section>"}

Then I plug the payload variable into the URLFetchApp code:

const url = "https://<the CMS>.com/api/v2/documents/1234";
const params = {
  headers: {
    Authorization: "Basic <token>",
    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":"<?xml version=\\"1.0\\"?><section xmlns=\\"http://docbook.org/ns/docbook\\" xmlns:xinfo=\\"http://ns.expertinfo.se/cms/xmlns/1.0\\" xml:id=\\"12345\\" version=\\"5.0\\" xml:lang=\\"en\\" xinfo:resource=\\"12345\\" xinfo:resource-id=\\"1234\\" xinfo:resource-type=\\"component\\" xinfo:resource-title=\\"Source 5\\" xinfo:resource-titlelabel=\\"\\" xinfo:version-major=\\"1\\" xinfo:version-minor=\\"0\\">  <title xinfo:text=\\"1234\\">Name</title>  <para>Short overview</para></section>"}

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":"<?xml version="1.0"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0" xml:id="12345" version="5.0" xml:lang="en" xinfo:resource="12345" xinfo:resource-id="1234" xinfo:resource-type="component" xinfo:resource-title="Source 5" xinfo:resource-titlelabel="" xinfo:version-major="1" xinfo:version-minor="0">  <title xinfo:text="1234">Name</title>  <para>Short overview</para></section>"}

This is the error returned by the CMS:

{"error":{"code":400,"type":"bad_request","message":"Invalid body."}}

I have tried:

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?

Thank you for any help and patience as I am not a programmer by profession.

Upvotes: 0

Views: 44

Answers (0)

Related Questions