4

In the Amazon API Gateway I have created a simple API containing a single resource called demo and one POST method corresponding to it:

enter image description here

Now, I want my endpoint to accept POST request of any Content-Type, so not necessarily application/json, but also plain/text. I then want to take the body of the request and wrap it in a JSON object and send it to an Amazon Lambda function (Lambda functions can only accept JSON object as parameter).

For this purpose I have edited the Integration Request corresponding to my method to use a custom template mapping:

enter image description here

I have used the reference from the Amazon documentation which can be found here: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

My Lambda function looks like this:

exports.handler = function(event, context) {
    context.succeed(event);
};

When testing, I get the expected output regardless of what I send:

enter image description here

However, when I deploy, the transformation just doesn't work anymore, it expects JSON

  • Sending anything yelds this:

enter image description here

  • Sending JSON yields this:

enter image description here

Is there any part of this process executed wrong? Am I missing something when deploying? To me, it looks like a very annoying Amazon bug, can anyone confirm that?

5
  • Does the "Test" button work? Commented Jul 21, 2015 at 16:03
  • Yes, but it is important to understand that when testing the Content Type of the request is forced to being application/json. This will in turn match my template. Commented Jul 21, 2015 at 17:04
  • And you can't remove the required Content-Type or replace it with '*'? Commented Jul 21, 2015 at 17:06
  • It doesn't seem that I can. Perhaps some other wildcard? Commented Jul 21, 2015 at 18:18
  • Thanks for this well written question, it helped me solve my issue Commented Oct 24, 2016 at 17:40

2 Answers 2

3

Use content type "application/json".

https://gist.github.com/maruks/e036168263cd412146e6

Sign up to request clarification or add additional context in comments.

3 Comments

Your solution is great and definitely better than what I was able to find in the documentation. However, this means that I need to know beforehand all the content types that I might receive. Is there way to specify something like a wildcard content type that will capture even requests without a set content type (or different ones all together)?
@Interfector I believe its not yet possible to have a default/wildcard handler. There is no official response yet from Amazon for this question on their forum.
The application/json is the default content-type. If no content-type has been specified in the request, the mapping template for application/json will be used to create the event for Lambda. If a content-type has been set in the request, then it seems that you need to have a template for each possible content-type.
1

Have you tried setting the Accept and Content-Type headers in your curl request? Below I'm assuming you are sending "text/plain" and can accept json in the response

-H "Content-Type: text/plain" -H "Accept: application/json"

1 Comment

I just tried that and it doesn't work. The accept part is redundant, I would just take in whatever was thrown at me. I also tried setting the Content-Type to application/json hoping I've missed some wizardry from Amazon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.