API Gateway not retrieving request body - amazon-web-services

I'm trying to create a POST endpoint in API gateway with request body in application/json type. Now, in input body mapping template, I want to check if the input is there and if the required fields are present or not. I tried getting input body using $input.body and also tried $input.json('$') and $input.path('$'). Nothing works, input body is always empty, although the $input.body == "" check always returns false. But in the test logs i can see that the body is passed through. I'm using Mock as integration type. What can be an issue?

Related

API Gateway not returning correct template

Having some weird AWS issue with mapping templates. My lambda either returns a json body or a 404. When the lambda returns a body this template works as intended but on the 404 I get nothing back when I’m expecting that "message": "test" from
#set($inputRoot = $input.path('$'))
{
"message" : "test"
}
My guess is that first line but nothing I change is giving me the desired result. My assumption so far is that since there's nothing being returned with the 404 it never substitutes this. Any ideas?
Edit: the 404 is coming from S3 directly through an integration request rather than a Lambda because it couldn't find anything to return. Rather than just pass that 404 to the customer we want to pass a message as well with details.

What is the difference between Params and Body in Postman

This might be a really simple question but I can't seem to find a clear answer.
In postman, what is the difference between the Params and the Body tabs?. I noticed that when I was trying to perform a PUT request, if I am to add the key and value in Params my PUT request doesn't seem to update what I intend to update but if I perform the PUT request in Body and have the form-data selected than the expected update happens.
So what exactly are the differences?
Params correspond to the request parameters that are appended to the request URL.they are most used with GET requests. On the other hand, Body is the actual request body (usually it defines the request payload or the data that needs to be processed by the request). PUT and POST requests read usually the data from the body of the request and not from the params.

AWS API Gateway integration request Http headers not being passed to lambda

This is similar to this post API-Gateway Integration Request HTTP Header not mapping query string to header but I can't see any answer for it and all of the answers are not related to what the question is intended.
I am trying to add the integration request parameters in API Gateway, but whenever I set 'Http Headers' as shown here and 'Mapping template' as passthrough, I could not see that header when I log inside the lambda.
Also in the integration response I cannot reference it inside the integration header response parameters. The "integration.response.header.cokers" will be returned blank when I invoke the API. This is how I configured the integration response
Ultimately the solution here is to implement a lambda proxy integration.
A proxy integration in API Gateway tells API Gateway to simply forward all headers to the integration for processing, which means you will see all of those values in your lambda function.
NOTE: Lambda has to return a specific response format back to API Gateway if it's a proxy integration. Ultimately, the response should be something like:
{
'statusCode': 200,
'headers': {
'echo-proxy': True
},
'body': 'some payload'
}
What you are trying to do right now is map everything manually, which is a deprecated approach and usually you don't want to do that unless you absolutely have to because it's kind of a pain.
If you have to map the headers manually start by mapping them in the the method request so it can carry on to the next step and so on. Basically like this:
Method Request -> Maps variable to Integration Request -> Maps variable to Body Mapping Template -> Maps variable to actual request header
What you have in your screenshot for the Integration Request -> HTTP Headers is:
Name: cokers
Mapped from: 'blah'
However, "mapped from" should look something like "method.request.header.coker" which is a standardized path (meaning to get the value from the Method Request Header field with name "coker").
Once you have added the coker header to the Method Request, and the Integration Request HTTP Headers are mapped correctly, you have to implement a mapping template. Set the content-type to application/json with passthrough set to "When there are no templates defined(recommended)" and a simple mapping template:
{
"headers": {
"coker" : "$input.params('coker')"
}
}
That is the way my API is setup and it returns the following to me because I had my lambda function return the event as a json object back to API GW:
{"body": "{\"headers\": {\"coker\": \"mapped\"}}", "statusCode": 200}
NOTE: the value of my header "coker" in the request on the client side is "mapped"
UPDATED ANSWER
To map the original "coker" header to "coker2" (or any other name you want to give it) you simply set the name of the header in your mapping template like so:
{
"headers": {
"coker2" : "$input.params('coker')"
}
}
Then edit your lambda function to return "coker2" header and you should get a response like this:
{"body": "{\"headers\": {\"coker2\": \"mapped\"}}", "statusCode": 200}

Postman Mock Server matching algorithm logic for request body param

I have two scenarios for the following API URL.
POST http://{{ip_port}}/oauth/token
When I put the user name and password correctly, it should return
200 and mock json response.
When I put user name and password incorrectly, it should return 401 and mocked json(error).
In Postman Mock server, I noticed that there is no matching algorithm logic for request param.
I want to filter by request param and return related mock responses. I don't want to add two URLs(/token and /failedtoken) for above scenarios.
Currently Postman only support three logic for matching algorithm logic.
Properly formatted responses
HTTP method
Filter by URL
Is there any way to add only one URL for many scenarios in Postman Mock Server?
Postman Mock Server now supports matching by request body. You can use it by specifying a custom header (x-mock-match-request-body to true).
You can also check out an example that demonstrates how this feature works by going to New->Templates and searching for Request Body Matching.

Using AWS API Gateway to build a patch method

I am using API Gateway to build a patch method.
In then Integration Request - Mapping Template i added:
{ "id": "$input.params('subscription-id')",
"env": "$stageVariables['env']",
"street": $input.json('street'),
"address_name": $input.json('address_name'),
"payment_day": $input.json('payment_day'),
}
As a patch http method, the user's API is not required to pass all the parameters.
So if the user doesn't pass, for e.g. payment_day, the field is going to be ''. The '' can be a valid value field. So i have two options:
Put a NULL value on the payment_day field.
Remove the payment_day from JSON request.
Is it possible to do this on API Gateway Integration Request -Mapping Template? Does anyone has a workaround?
You can use Velocity Conditionals to only output optional values if present.
Alternately, as mentioned in comments, you can just pass the entire JSON body using $input.json('$') and handle the presence or lack there of inside your Lambda function.