I need to fetch some data from ApiGateway endpoint and then based on response store this data in database.
I created simple Lambda function that just fetch data from ApiGateway endpoint and print it in console. My first Lambda function did not have any VPC configuration and fetch operation worked like a charm.
const fetch = require('node-fetch');
exports.handler = async () => {
const data = await fetch("https://<<ag-api-key>>.execute-api.us-east-1.amazonaws.com/v1/data");
const response = await data.json();
console.log(data, response);
}
As I need to store data received from endpoint into database which run under VPC I decided to put Lambda in same VPC(this vpc has configured Internet Gateways and other stuff to have access to internet).
As a result fetch operation start to fail with 403 response code and {"message":"Forbidden"} response body.
Api Gateway resource does not have any custom domain configuration and maintained by other team so I do not have direct access to its configuration
May be anyone can suggest how I can fix this
Security Groups, check if port 443 is open
Check your CORS Setting on API Gateway.
Try Hitting the API Gateway with Postman/Fiddler, or any other testing tool to make sure your API Gateway is online and you can get the response you want.
If you are using a Private API-Gateway (Sounds like you are using public looking at the URL) you will need some header data and different URL. I can guide you through it if needed. I would avoid private API gateway if I were you.
Let me know if any of that helps. I have ran into that issue many times in different situations.
Related
I have an API Setup in API Gateway with 2 routes currently: Login and Register which are both attached to AWS Lambdas to perform operations on a DynamoDB Table.
When I perform a Post Request on Postman, I can see my custom response Body in the body of the response i get back. I'm using this to get back a jwt token hence the need for custom body.
However, in my React Native code:
I get a correct status code in the response of 200 but if i log out response.body i get "undefined"...
Edit: There is also no sign of the jwt token under any other name in the response when thats logged out as a whole too.
any help would be appreciated! Thanks
EDIT: EXTRA PHOTOS
Had to use
await for response.text()
to get the desired data out of the response, not just a single await on the whole response. Solved by #ThibaultCapelli
I'm in pursue of solution how to fetch data from API "PVGIS" (https://re.jrc.ec.europa.eu/api/) using vanilla javascript and process data for futher calcualtions
when accessing api from browser js script i get an "CORS policy error"
when accessing API from standalone node.js script i can fetch a data
here is fetch function from node that outputs data correctly
async function fetchData() {
const data = await fetch('https://re.jrc.ec.europa.eu/api/PVcalc?lat=45&lon=8&peakpower=1&loss=14&outputformat=json')
.then(response => response.json())
.then(json => {
console.log(json.outputs.monthly)
})
}
as i understand, i should be building a backend server side with api request to pvgis and then connecting it with my front js side to process the data ... is this a correct path?
Can you give me some details how to get going ?
here is the GUI of this api
https://re.jrc.ec.europa.eu/pvg_tools/en/tools.html
and API documentation
https://joint-research-centre.ec.europa.eu/pvgis-online-tool/getting-started-pvgis/api-non-interactive-service_en
As the website says, they are not making this API's available for use in browser
Warning: access to PVGIS APIs via AJAX is not allowed. Please, do not ask for changes in our CORS policy since these requests will be rejected by the system administrators.
They basically have CORS policy which blocks any other origin to make request. Cors is basically browser security mechanism. But as you mentioned you can access it through script since its not in browser.
Also as they mentioned, they have rate limiters so you can not call api every second.
Way to go here would be to have backend script on nodeJS which calls api, gets data and saves it in your database and then you would have another api endpoint, that queries this data from database. Also add some cron job, which would schedule request to get newer data from api every hour or so, so your db data does not go stale
I try to build a movies library with react js
I take the data from an API, and add Search component
when I type a text in the search bar I have an error,
no results appeared! and there is an error in the console!
isAxiosError.js:10 Mixed Content: The page at 'https://react-
japox3.stackblitz.io/' was loaded over
HTTPS, but requested an insecure XMLHttpRequest endpoint
'http://www.omdbapi.com/?
i=tt3896198&apikey=b0cdc816&s=g'. This request has been blocked; the content
must be served over
HTTPS.
isAxiosError.js:10 Uncaught (in promise) Error: Network Error
at e.exports (isAxiosError.js:10)
at XMLHttpRequest.l.onerror (isAxiosError.js:10)
can you help me to solve it?
this is the code and you can try it
https://stackblitz.com/edit/react-japox3?file=src%2FApp.js
Your problem is that your site is secure HTTPS whereas you are requesting from the omdbapi which is insecure HTTP
I would suggest you to use some secure API for the purpose over HTTPS.
Or you could do it without a API too.
Here is a module called moovies that might help.
With this you can setup a local api server just with one line of code.
const moovies = require('moovies')
const express = require('express')
const app = express()
app.use('/apipath', moovies.mooviesBackend)
app.listen(8000, () => {
console.log("Server is listening on port 8000");
})
You can also use the methods moovies.getPlot('tt3896198', callback) or moovies.getCast('tt3896198', callback) to get the same json responses directly from the code without any api.
The callback should be like:
function callback(response, error){
//do your stuff here
}
This library is being updated regularly and new features are getting added.
Disclosure: I am the developer of the Library.
I have a react application using ES6 fetch API to call a rest endpoint using CORS. The fetch call work just fine, but I'm unable to get the response headers that are being sent from the server. I can see the response headers in Chromes devtools so I know they are being sent back to me; however access to them in code doesn't seem to work for me. Here is the code (its basic fetch promise-chaining):
fetch(url)
.then(response => {
for (let header of response.headers) { < -- - headers is empty
console.log(header);
}
return response;
});
But response.headers is empty. But I can clearly see the headers in Chrome. Is there a reason why the fetch call is blocking me from viewing them? What do I need to get access to these headers? The server stores some application specific headers that we would love to use.
UPDATE: I was able to fix this by having the server add this to the response of the OPTION request:
response.setHeader("Access-Control-Expose-Headers", "X-Custom-Header");
Now on the response of the fetch command, I can do the following:
const customHeader = response.headers.get('X-Custom-Header');
Thank you Karim for the help on this!
To get a specific header you need to call response.headers.get(key)
and the iterable object is response.headers.entries and not response.headers
for (let header of response.headers.entries()) {
console.log(header);
}
https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
In addition, in a cors scenario only some headers are exposed to the app, thus not all the headers you see on the chrome dev tools are necessarily available at application level.
For further details check this answer:
Reading response headers with Fetch API
I'm trying to implement push notification on my site with AWS SNS https protocol but am having trouble figuring out how to receive the token returned from the subscribe method. What is an example of a handler that processes the confirm sub POST request from SNS? I'm currently proving the browser's endpoint as the param to subscribe, is that the right endpoint? I've gone through the SNS Doc multiple times but can't seem to wrap my head around it.
this is the response i'm getting from the subscribe method:
{ ResponseMetadata: {…}, SubscriptionArn: "pending confirmation" }
Your code should read the HTTP headers of the HTTP POST requests that Amazon SNS sends to your endpoint. Please see [Source] for the HTTP header example.
Read the value for SubscribeURL and visit that URL. To confirm the subscription and start receiving notifications at the endpoint, you must visit the SubscribeURL by HTTP Get request.
if (message['Type'] === 'SubscriptionConfirmation') {
https.get(message['SubscribeURL'], function (res) {
// You have confirmed your endpoint subscription
});
}
Source: https://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html#SendMessageToHttp.confirm