Open weather map api not working

I am trying to make a weather app but I am getting this error.


The API key is valid as I have tried using it (https://api.openweathermap.org/data/2.5/weather?q=Trivandrum&appid=658767b0ae936b022f59a69f44868419&units=metric)

My code:-

const express =require("express");
const https=require("https");
const bodyParser=require("body-parser");
const app=express();
app.use(bodyParser.urlencoded({extended: true}));

app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html");
});
app.post("/",function(req,res)
{
  const query=req.body.CityName;
  const api_key="658767b0ae936b022f59a69f44868419"
  const unit="metric";
const url="https://api.openweathermap.org/data/2.5/weather?q="+query+"&appid="+"&units="+unit;
https.get(url,function(response){
  console.log(response.statusCode);
  response.on("data",function(data)
{
const weather_data=  JSON.parse(data);
console.log(weather_data);
const temp=weather_data.main.temp
const icon=weather_data.weather[0].icon
const icon_id=" http://openweathermap.org/img/wn/"+icon+"@2x.png"
res.write("Temp="+temp);
res.write("<img src="+icon_id+">");
res.send();
})
});
})

@Sapna2001 There is nothing wrong with your API key. However, there is a big problem with the following line:

const url="https://api.openweathermap.org/data/2.5/weather?q="+query+"&appid="+"&units="+unit;

You should probably add a console.log statement after this line, if you do not see the problem right off.

1 Like