Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for contentHandling - Fixes gh-6949 #6987

Merged
merged 1 commit into from Nov 22, 2019
Merged

Add support for contentHandling - Fixes gh-6949 #6987

merged 1 commit into from Nov 22, 2019

Conversation

tinexw
Copy link
Contributor

@tinexw tinexw commented Nov 20, 2019

What did you implement

Option to set contentHandling property of API gateway method integration and integration response.

Closes #6949

How can we verify it

service: content-handling

provider:
  name: aws
  logs:
    restApi:
      level: INFO
  runtime: nodejs8.10
  region: eu-central-1
  apiGateway:
    binaryMediaTypes:
      - 'image/jpeg'

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          method: POST
          path: hello
          integration: lambda
          request:
            contentHandling: CONVERT_TO_TEXT
            template:
              image/jpeg: '{"method": "$context.httpMethod","bodyLength": $input.body.length(),"body" : "$input.body"}'
          response:
            headers:
              Content-Type: "'image/jpg'"
            contentHandling: CONVERT_TO_BINARY
// handler.js
'use strict';

const fs = require('fs');

module.exports.hello = async (event) => {
  console.log(event)
  return fs.readFileSync('./image.jpg').toString('base64');
};

image.jpg

Steps:

  • sls deploy
  • curl -XPOST -H "Content-Type: image/jpeg" -H "Accept: image/jpeg" https://<id>.execute-api.eu-central-1.amazonaws.com/dev/hello -d @image.jpg --output response.jpg
  • response.jpg should be the same asimage.jpg

Todos

Useful Scripts
  • npm run test-ci --> Run all validation checks on proposed changes
  • npm run lint-updated --> Lint all the updated files
  • npm run lint:fix --> Automatically fix lint problems (if possible)
  • npm run prettier-check-updated --> Check if updated files adhere to Prettier config
  • npm run prettify-updated --> Prettify all the updated files
  • Write and run all tests
  • Write documentation
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@tinexw tinexw closed this Nov 20, 2019
@tinexw tinexw reopened this Nov 20, 2019
@codecov-io
Copy link

Codecov Report

Merging #6987 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #6987   +/-   ##
=======================================
  Coverage   88.43%   88.43%           
=======================================
  Files         229      229           
  Lines        8420     8420           
=======================================
  Hits         7446     7446           
  Misses        974      974
Impacted Files Coverage Δ
...ompile/events/apiGateway/lib/method/integration.js 98.14% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 05eec83...08f55eb. Read the comment docs.

Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tinexw It looks great!

Still are you sure it fixes #6949, it doesn't look related to me, but maybe I miss something (?)

@tinexw
Copy link
Contributor Author

tinexw commented Nov 21, 2019

Still are you sure it fixes #6949, it doesn't look related to me, but maybe I miss something (?)

Yes. See my second comment in the issue.
You can also verify this by removing the line contentHandling: CONVERT_TO_TEXT from the serverless.yml I gave above. If you create a new stack without this line and execute the curl command, you will get a 500 error and in the API gateway logs you see

(301daa80-24e8-41b2-a819-fcea915035a9) Execution failed due to configuration error: Unable to transform request

Copy link
Contributor

@medikoo medikoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tinexw for clarifications. Looks great! Taking this in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request with binary media returns 500
4 participants