I have the following simple test:
const jsObj = {description: `a b`}; const expected = 'description=a%5Cnb'; const actual = Utils.buildQueryParams(jsObj); assert.deepEqual(actual, expected); Somewhere in the depths of Utils.buildQueryParams each parameter placed on the summary line goes through the following procedure:
function encodeQueryItem(item) { let escaped = item; if (typeof escaped === 'string') { escaped = JSON.stringify(escaped); escaped = escaped.substring(1, escaped.length - 1); } return encodeURIComponent(escaped); } Everything works encodeQueryItem for me, but I don’t like what the encodeQueryItem looks encodeQueryItem . Is it possible to do this in a more elegant way?
Once the “why” questions have been sent, then I will explain: If I have a line like a\nb , where \n is a newline character, then the following line will go to the server: 'description=a%0Ab' , and in the same form (after URL decode) will be saved there. Then, when I ask him to return this line to me, he will happily put it in JSON, and the JSON parser will break because all whitespace characters in the string must be escaped.
Although it is not important, here is the code and buildQueryParams :
function buildQueryParams(jsObject) { return Object.keys(jsObject) .map((name) => `${name}=${encodeQueryItem(jsObject[name])}`) .join('&'); }