The nodejs function is expected to return text/csv but instead returning quoted string with embedded newline character \n instead of real newline.
Using interceptor for headers received in response it states correctly content-type: text/csv for request like GET /tst/items/name HTTP/1.1 The nodejs snippet is
const Json2csvP = require('json2csv').Parser;
const fields = ['f1','f2','f3'];
var searchQ =['select * from names'];
await db.any(searchQ[0])
.then(function(data) {
const json2csvP = new Json2csvP({ fields, quote: '', eol: '\n' });
console.log(csv);
callback(null, (csv));
})
so for the console response in AWS output is like
"field1, field2 field3\n1 2 3"
and the caller in browser/REST tool get it
"field1, field2 field3\n1 2 3"
but I am looking the below csv format at callee side
field1 field2 field3
1 2 3
note there is no quote, and a real new line instead of \n character in plain text without html.I did tried using .split like below
callback(null, (csv).split(/\n/g));
that give real newline only in AWS response result ["field1 field2 field3", "1 2 3"]
but not in the format desired (without quotes in both line) and for the caller/ browser/REST tool , it gave like below without newline and with quotes and extra undesired [ and ] characters like like below.
["field1 field2 field3", "1 2 3"]
How can caller receive the below?
field1 field2 field3
1 2 3
"field1, field2 field3\n1 2 3"probably because it's JSON-encoding the string. The data behind that textual representation is what you think (no quotes, with a "real" newline). Try the following in Node.js:var fs = require('fs'); var csv = "field1, field2 field3\n1 2 3"; fs.writeFileSync('out.csv', csv);and then look atout.csv. It matches your expectation. Everything is fine.JSON.parse(theWeirdString), that should give you a plainly-coded string.bodystring from the response object would be returned to the caller, and it would look exactly as you expect.