Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Gateway validate content-type header

IF the content type isn't json api gateway does no validation and just passes through. Im using the proxy integration so dont have mapping templates and can only use "when_no_match". Im using inline swagger.

I can check if the header exists but not check the value- how do I do that?

/myMethod:
    post:
        x-amazon-apigateway-request-validator : "myvalidator"
        parameters:
        # How do I also validate Content-Type value is "application/json"
        - name: Content-Type
          in: header
          required: true

I want API gateway to validate this so I dont have to check it in code

like image 439
red888 Avatar asked May 31 '26 08:05

red888


1 Answers

Go to the Integration Request tab of your endpoint, click Mapping Templates, set Request body passthrough to never, add a mapping template for application/javascript, and click Method Request Passthrough from the dropdown next to Generate template.

Here's the swagger snippet: requestTemplates: application/json: | ## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html ## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload #set($allParams = $input.params()) { "body-json" : $input.json('$'), "params" : { #foreach($type in $allParams.keySet()) #set($params = $allParams.get($type)) "$type" : { #foreach($paramName in $params.keySet()) "$paramName" : "$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end #end } #if($foreach.hasNext),#end #end }, "stage-variables" : { #foreach($key in $stageVariables.keySet()) "$key" : "$util.escapeJavaScript($stageVariables.get($key))" #if($foreach.hasNext),#end #end }, "context" : { "account-id" : "$context.identity.accountId", "api-id" : "$context.apiId", "api-key" : "$context.identity.apiKey", "authorizer-principal-id" : "$context.authorizer.principalId", "caller" : "$context.identity.caller", "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", "cognito-identity-id" : "$context.identity.cognitoIdentityId", "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", "http-method" : "$context.httpMethod", "stage" : "$context.stage", "source-ip" : "$context.identity.sourceIp", "user" : "$context.identity.user", "user-agent" : "$context.identity.userAgent", "user-arn" : "$context.identity.userArn", "request-id" : "$context.requestId", "resource-id" : "$context.resourceId", "resource-path" : "$context.resourcePath" } } passthroughBehavior: "never" httpMethod: "POST" type: "aws"

like image 197
johnktims Avatar answered Jun 02 '26 22:06

johnktims



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!