最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

amazon web services - AWS API Gateway integration response template mapping for step functions - Stack Overflow

matteradmin5PV0评论

I have been integrating express step functions to API Gateway endpoints and have run into an issue where I am returning following object

{
  "statusCode":"500",
  "body":{
    "message: "Internal Server Error"
   }
}

OR

{
  "statusCode": "200",
  "body": {
    "result": {...}
  }
}

I want to be able to map the statusCode to HTTP status and the body to HTTP response body. I have been able to map the statusCode to the HTTP status as follows,

##velocity template

#set($inputRoot=$input.path('$'))

#set($output=$util.parseJson($inputRoot.output))

#set($context.responseOverride.status=$output.statusCode)

this works but when I try to return the body, what I get in response (in Postman) is:

{message=Internal Server Error
}

I have been integrating express step functions to API Gateway endpoints and have run into an issue where I am returning following object

{
  "statusCode":"500",
  "body":{
    "message: "Internal Server Error"
   }
}

OR

{
  "statusCode": "200",
  "body": {
    "result": {...}
  }
}

I want to be able to map the statusCode to HTTP status and the body to HTTP response body. I have been able to map the statusCode to the HTTP status as follows,

##velocity template

#set($inputRoot=$input.path('$'))

#set($output=$util.parseJson($inputRoot.output))

#set($context.responseOverride.status=$output.statusCode)

this works but when I try to return the body, what I get in response (in Postman) is:

{message=Internal Server Error
}
Share Improve this question edited Nov 16, 2024 at 9:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 16, 2024 at 4:16 PratikPratik 3072 silver badges9 bronze badges 4
  • What specific body output are you aiming to achieve? Based on the description, it seems the body already meets your requirements. – Gerard Haw Commented Nov 16, 2024 at 4:24
  • I want it in Json format. { "message": "Internal Server Error" }, as of now I am getting message=..., – Pratik Commented Nov 16, 2024 at 21:21
  • Basically I want to map Body as is to Http Response Body – Pratik Commented Nov 16, 2024 at 21:23
  • Can you provide some API Gateway logs? – fa44 Commented Nov 19, 2024 at 21:33
Add a comment  | 

1 Answer 1

Reset to default 0

The issue is that you have parsed the json in the template, what you want is:

##velocity template

#set($output=$input.json('$.output'))

#set($context.responseOverride.status=$input.path('$.output.statusCode')

https://docs.aws.amazon/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference

The .json function of the input object will convert it to a json string istead of an object internally in VTL

Post a comment

comment list (0)

  1. No comments so far