Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting not found http error with Openweathermap API

I'm trying to code a weather app that displays temperature according to your current location ,using OpenWeatherApi, I get an error 404 I don't know why

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(position => {
      long = position.coords.longitude;
      lat = position.coords.latitude;
      console.log(position);
      const api = `api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=74913cd6ec927a299e7fd99534e17b5f`;
      fetch(api)
        .then(response => {
          return response.json();
        })
        .then(data => {
          console.log(data);
        });
    });

The error I'm getting in the console:

error I get in the console

like image 620
imeneblmi Avatar asked Apr 15 '26 11:04

imeneblmi


1 Answers

You need to prepend https:// to your URL. For example,

api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key}

https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key}

The API call on the OpenWeather occasionally excludes https:// from their API call URLs on their website. However when making an API call in your JS code, you cannot omit this because otherwise your code will convert your URL to a relative path.

like image 195
kelvin Avatar answered Apr 18 '26 00:04

kelvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!