AWS API Gateway routes request to $default instead of route - amazon-web-services

I am using an AWS Websocket API Gateway that has the following routes:
I've been able to connect to my websocket and send requests and receive responses from /SendMessage with the following json: {"action": "SendMessage", "message": "Hello, World"} however, when I tried adding a new route /Register. Sending the json {"action": "Register", "message": "Hello, World"} AWS API Gateway routes the request to $default.
The following request appears on CloudWatch:
The request should be routed to /Register and not /default. Do I need to do some kind of redeployment of the API Gateway when I add a new route?

First, make sure you have re-deployed your API Gateway.
Second, make sure you have re-deployed your Lambda functions
Last, you have to Stringify your request like this:
socket.send(JSON.stringify({
"action": "SendMessage",
"message": "Hello, World"
}))

Related

Access to XMLHttpRequest has been blocked by CORS policy using custom domain: AWS API gateway

I have a custom domain linked to my test stage in the AWS API gateway.
I have a method as seen in the attached picture and have enabled cors (as also attached in the picture.
The endpoint will call my lambda function (with proxy lambda enabled as well)
I call the api endpoint from my Ionic angular application like so:
axios.defaults.headers.post['Content-Type'] = 'application/json';
return axios.post(url, {
data
}).then((response) => {
if (response.data.status == 'success') {
});
but in my console I get the following error:
Access to XMLHttpRequest at 'https://test.mycustomendpoint.com/auth/v1/?action=login' from origin 'http://localhost' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
I am wondering what I may be doing wrong.
Image11
Image22
This problem only happens when you run the app using serve to solve it just install this chrome extension on your device from here

The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer

I created a REST API with AWS API Gateway. I have a GET method that triggers a Lambda function, but I cannot get the response I need. I properly configured query params etc. but in Lambda logs I see this.
com.amazonaws.serverless.exceptions.InvalidRequestEventException: The incoming event is not a valid request from Amazon API Gateway or an Application Load Balancer
2021-05-19T15:37:38.003+04:00 at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:48)
2021-05-19T15:37:38.003+04:00 at com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader.readRequest(AwsProxyHttpServletRequestReader.java:30)
2021-05-19T15:37:38.003+04:00 at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:201)
What am I missing ? Thank you in advance.
When running lambdas locally, you need to define the event that trigger it.
For example:
sam local invoke --event tst\event.json where the event.json is basically the http parameters you are passing.
{
"httpMethod": "GET",
"path": "/",
}
For creating an event json template file for http requests, you can use: sam local generate-event apigateway aws-proxy.

AWS - Api Gateway POST body with Content-Type: text/plain instead json/application to lambda

REQUEST: POST body to ApiGateway to Lambda with Content-type:text/plain
RESPONSE: "message": "Internal server error"
Body example:
{"a":"first", "b":"second"}
Which configuration did i need to change to accept this Content-Type?
For json/application, it works just fine.
When creating a resource, use lambda proxy integration.
For more information read here.

AWS Lambda and API Gateway response integration issue

I have deployed AWS stack with a Lambda function and an API gateway. After deployment I tested Lambda function independently and it works fine but when I invoke it using AWS API Gateway, it fails with `
Internal Error: 502
I looked at cloudwatch logs and it says
Endpoint response body before transformations: null
I am returning my response from Lambda (Python 3.6) in following way -
body = {
"message": "Success!!"
}
response = {
"statusCode": 200,
"headers": {
"content-type": "application/json"
},
"body": json.dumps(body),
"isBase64Encoded": False,
}
return response
Ok, I found the problem. Actually the code is correct. I made few changes and took the above code outside handler. So my handler was calling this new function and it was returning response to handler but I missed to return the received response again from handler to API gateway.

AWS API Gateway, rewriting discoverability URLs in response body

Got legacy microservices running on EC2 instances accessed externally via API Gateway. Microservices return JSON responses with discoverability url options:
GET /api/accout/0001
{
id: "0001",
balance: 1000000,
currency: "BTC",
_links: [
{name: "close", method: "DELETE", url: "http://10.0.0.1:8080/api/account/0001" },
{name: "deposit", method: "POST", url: "http://10.0.0.1:8080/api/account/0001/deposit" }
]
}
API Gateway exposes the API via https and a custom domain name so links like "http://10.0.0.1:8080/api/account/0001" make no sense externally.
How do I configure AWS API Gateway to replace "http://10.0.0.1:8080/api/account/0001" with "https://api.mycompany.com/api/account/0001" before passing responses from microservices to external api clients?
API Gateway supports Response transformation and method mapping functionality.
You can define the response templates with required transformation:
API Gateway Response transformation
Based on this:
If needed, add body-mapping templates to transform given integration
response payloads into specified method response payloads.
Specifically Method Models which use JSON path expressions, which can be used for transformation.