Accept only Content-Type: 'application/json' for a HTTP event

Hi,

Even though I include a validation schema other types are accepted by the API Gateway after deploying to AWS. For example plain texts are accepted. Of course error handling can be done for that. But is there any way to accept only a JSON data to the API. If so, how can I include it in the serverless.yml. I checked the documentation and found Lambda Integration (https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-integration). But how can I add accept header in serverless.yml?

Thank you

2 Likes

I am also looking for the answer to this! Did you get anywhere with it?
Thanks!

Hi @sajithneyo @gaiachik,

Not sure whether i understood your requirement correctly. Correct me if i’m wrong, your requirement is accept only of content-Type application/json and how to achieve the same using serverless correct?

If i understood correctly means,

    service: Test-API

provider:
name: aws
runtime: nodejs10.x
description: Test API for content-Type # optional, Description to publish to AWS
region: us-east-1
stage: Test-dev

functions:
GetCart:
handler: handler.hello
events:
- http:
path: /test1
method: get
authorizer: aws_iam
integration: http
cors: false
request:
uri: ‘http://karthibalu.simplesite.com
passThrough: NEVER
template:
application/json: ‘{}’
parameters:
headers:
X-Ep-User-Roles: false
X-Ep-User-Scopes: false
X-Ep-User-Traits: false
X-Requested-With: false
X-Request-Id: false
X-Ep-User-Id: false
response:
statusCodes:
200:
pattern: ‘’
headers:
Strict-Transport-Security: “‘max-age=31536000’”
Access-Control-Expose-Headers: “‘cLocation’”
Access-Control-Allow-Origin: “‘Original Url’”
cLocation: integration.response.header.Location
500:
pattern: ‘500’
headers:
Strict-Transport-Security: “‘max-age=31536000’”
Access-Control-Allow-Origin: “‘Original Url’”

Let me know if this suits the requirement. !!!

Thanks, Always welcome for needy.

Hello (and thank you)
Yes. I realized that actually my question was different, because i am using lambda-proxy integration. I realized later that you can do it with the lambda one and NEVER as option for pass through.
What I’ve done right now with lambda-proxy integration feels hacky, i.e. i just check the headers in the handler and return a 400 if they don’t match.
I was hoping to find a way to do it without using lambda integration, but i could not find any info on how to do it with lambda-proxy. Maybe it’s not a possible setting.
I did open a separate topic for my question (in case you know the answer to that, too! :slight_smile: Reject requests based on content-type header, lambda-proxy

Thank you
Gaia