aws / aws-sdk-ruby

The official AWS SDK for Ruby.
https://aws.amazon.com/sdk-for-ruby/
Apache License 2.0
3.56k stars 1.22k forks source link

Lambda response wraps string in an extra set of quotes, triple escapes chars #1897

Closed lizdenhup closed 5 years ago

lizdenhup commented 5 years ago

Please fill out the sections below to help us address your issue

Issue description

I am working on an aws lambda that takes in some data from a file and outputs html. The html that is outputted has two sets of double quotes around it, and other characters are triple-escaped. What is the proper way to avoid this, or sidestep this problem? Thanks.

Gem name

aws-sdk-lambda, v1.11

Version of Ruby, OS environment

ruby 2.4.4 Mac OS 10.13.4

Code snippets / steps to reproduce

Here is a simplified example of the lambda:

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('<table class="foo"><tr><td></td></tr></table>')
    };
    return response;
};

Here is the response returned after successfully invoking the lambda:

{
  "statusCode": 200,
  "body": "\"<table class=\\\"foo\\\"><tr><td></td></tr></table>\""
}
awood45 commented 5 years ago

To help debug this, can you show the response you received in wire trace? Turning on http_wire_trace: true on your Aws::Lambda::Client class should provide the wire trace. That will help us determine where the formatting is being applied.

lizdenhup commented 5 years ago

Hi, thanks for your quick response! Here are the logs after setting http_wire_trace: true on the instance of the Aws::Lambda:Client class.

starting SSL for lambda.us-east-1.amazonaws.com:443...
SSL established
<- "POST /2015-03-31/functions/excel-to-html-converter/invocations HTTP/1.1\r\nContent-Type: \r\nAccept-Encoding: \r\nUser-Agent: aws-sdk-ruby3/3.32.0 ruby/2.4.4 x86_64-linux aws-sdk-lambda/1.11.0\r\nHost: lambda.us-east-1.amazonaws.com\r\nX-Amz-Date: 20181022T221332Z\r\nX-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\r\nAuthorization: AWS4-HMAC-SHA256 Credential=<<redacted>>, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=4df02e4c6d7b659a9a632878590e71687b60edbe2353a4b1a4d6e9610a585110\r\nContent-Length: 0\r\nAccept: */*\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "Date: Mon, 22 Oct 2018 22:13:27 GMT\r\n"
-> "Content-Type: application/json\r\n"
-> "Content-Length: 53\r\n"
-> "Connection: keep-alive\r\n"
-> "x-amzn-RequestId: b379188c-d647-11e8-8c8e-65114965fb2f\r\n"
-> "x-amzn-Remapped-Content-Length: 0\r\n"
-> "X-Amz-Executed-Version: $LATEST\r\n"
-> "X-Amzn-Trace-Id: root=1-5bce4b87-9954bf6b36d397b4c73ba36a;sampled=0\r\n"
-> "\r\n"
reading 53 bytes...
-> "\"<table><tr class=\\\"strong\\\"><td>2</td></tr></table>\""
read 53 bytes
Conn keep-alive
awood45 commented 5 years ago

The wire trace logs there show me that the triple-escaping happened before the response was sent to the Ruby SDK, we're faithfully returning what the service gave us.

What I'd recommend doing is looking at your Lambda handler's implementation, and possibly reaching out to AWS Support. I don't know which runtime you're invoking, so I may not have the expertise to help debug your Lambda handler.

Closing as there's nothing much we can do on the Ruby SDK end. Feel free to reach out if you have more questions or see further issues after the wire trace shows the output you want.