1

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
8
  • Everything is fine. AWS console is printing "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 at out.csv. It matches your expectation. Everything is fine. Commented Apr 19, 2018 at 3:24
  • Thanks Ahmed, there is more on this and it is not ok for the caller like REST client that expect data in desired format. The file get downloaded without using fs lib in browser (see the header in response, it is text/csv) and when I open it both in notepad and notepad++, the output is a quoted string with embedded newline character. In browser(chrome) the page display quoted string with embedded \n chacter. The same code in java or c# gave desired result but in nodejs, looks somerhing is missing from above code. Commented Apr 19, 2018 at 3:52
  • I see, then try JSON.parse(theWeirdString), that should give you a plainly-coded string. Commented Apr 19, 2018 at 3:54
  • Yes, I tried JSON. parse and it gave null. Commented Apr 19, 2018 at 4:01
  • 1
    Your Lambda function is returning your output string as a JSON string because that is what Lambda functions always return -- some kind of JSON. It sounds like you are using API Gateway, in which case, a Lambda Proxy Integration seems like what you should be using, because the response JSON object is deserialized by API Gateway and the body string from the response object would be returned to the caller, and it would look exactly as you expect. Commented Apr 20, 2018 at 0:49

1 Answer 1

1

I did tried many nodejs modules for parsing, csv and streams etc. to get the desired ouput and none worked. The outuput is ALWAYS stringified.

As per AWS documentation, it appear to be nodejs and Aws combined limitation https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

result – is an optional parameter that you can use to provide the result of a successful function execution. The result provided must be JSON.stringify compatible. If an error is provided, this parameter is ignored

.

This limitation is not there for java and Aws combined. https://docs.aws.amazon.com/lambda/latest/dg/java-programming-model-handler-types.html

outputType – If you plan to invoke the Lambda function synchronously (using the RequestResponse invocation type), you can return the output of your function using any of the supported data types.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.