Im trying to customize the error response in AWS API gateway using x-amazon-apigateway-gateway-responses . I see we can use $context to fetch error details. But How do I fetch the field Name/parameter name from the requestBody.
Here is my swagger code
`"/postStudent": {
"put": {
"operationId": "postStudent",
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [
{
"name": "x-request-id",
"in": "header",
"required": true,
"type": "string"
},
{
"name": "StudentRequest",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/StudentRequest"
}
}
],
"responses": {
"200": {
"description": "200 response",
"headers": {
"x-request-id": {
"type": "string"
}
}
},
"400": {
"description": "400 response",
"headers": {
"x-request-id": {
"type": "string"
}
}
},
"500": {
"description": "500 response",
"headers": {
"x-request-id": {
"type": "string"
}
}
}
},
"security": [
{
"api_key": []
}
],
"x-amazon-apigateway-request-validator": "Validate body, query string parameters, and headers"
}
}
},
"definitions": {
"StudentRequest": {
"title": "StudentRequest",
"type": "object",
"properties": {
"studentRollNumber": {
"type": "integer",
"format": "int32"
},
"studentName": {
"type": "string"
},
"section": {
"type": "string"
}
},
"required" : ["studentRollNumber"]
}
},
"x-amazon-apigateway-gateway-responses": {
"BAD_REQUEST_BODY": {
"statusCode": 400,
"responseTemplates": {
"application/json": "{\r\n \"message\":\"Invalid field\", \r\n \"code\":\"677\"}"
}
},
}`
I want to fetch the field name **studentRollNumber **from the requestBody object if a invalid value is passed and return in the error response body as { message: Invalid Field studentRollNumber , code:677}
I tried using the following but none of them gives the requestBody parameters value
'$context.error.responseType', '$context.error.message', '$context.error.validationErrorString', '$context.resourcePath', '$context.error.messageString', '$context.extendedRequestId'
I referred to this documentation:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference-access-logging-only