0
votes

I've tried all sorts of ways to get weather data from open weather if I paste this into the browser I get data http://api.openweathermap.org/data/2.5/weather?q=London&appid=6e93b3d15872f914c6929fed9ea71e9a

but if I use it with fetch or Axios or various other methods that work for me with other API's I get nothing back. Can anyone tell me what I am doing wrong? thanks

4
thanks everyone - it seems it was not having https that was the issue (I was working with repl.it) - Ron Southin
Yes, it works fine. - Suresh Mangs

4 Answers

0
votes

Use https instead of http. You will get data.

0
votes

Try this

const response = await fetch("http://api.openweathermap.org/data/2.5/weather?q=London&appid=6e93b3d15872f914c6929fed9ea71e9a");

const data =  await response.json();

console.log(data);

Also wrap you code inside try/catch to catch any possible errors.

You can also use promises instead of async/await -

fetch("http://api.openweathermap.org/data/2.5/weather?q=London&appid=6e93b3d15872f914c6929fed9ea71e9a")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => {
  console.log(error);
});

Also it will be better if you provide solutions that you have already tried in the question.

0
votes

index.html

<html lang="en">

<head>
    <title>Document</title>
</head>

<body>

</body>

<script>
    fetch('http://api.openweathermap.org/data/2.5/weather?q=London&appid=6e93b3d15872f914c6929fed9ea71e9a')
        .then(data => data.json())
        .then(data => {
            console.log(data);
        })
</script>

</html>