Thursday, March 5, 2020

Get Weather Report API by City name using ReactJS


Method: GET
API URL: http://api.openweathermap.org/data/2.5/weather?q=Pondicherry&appid=9de243494c0b295cca9337e1e96b00e2

by adding Params

q - Pondicherry (City name)
appid - 9de243494c0b295cca9337e1e96b00e2 (Register in openweathermap.org and get your own API ID)

const inputWeatherData = {
method:"POST",
url: "http://api.openweathermap.org/data/2.5/weather?",
data:JSON.stringify(),
headers:{
"Content-Type":"application/json",
},
params:{
"q": "Pondicherry",
"appid":"9de243494c0b295cca9337e1e96b00e2"
}
}

axios(inputWeatherData).then(res => {
if (res.data.weather){
console.log("weather result: ", res.data.weather[0].main);
} else {
console.log("weather result: NO Result Found");
}
})

Sample Output:

{
    "coord": {
        "lon": 79.83,
        "lat": 11.93
    },
    "weather": [
        {
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 300.09,
        "feels_like": 300.22,
        "temp_min": 300.09,
        "temp_max": 300.09,
        "pressure": 1011,
        "humidity": 78,
        "sea_level": 1011,
        "grnd_level": 1011
    },
    "wind": {
        "speed": 7.13,
        "deg": 137
    },
    "clouds": {
        "all": 44
    },
    "dt": 1583421191,
    "sys": {
        "country": "IN",
        "sunrise": 1583369637,
        "sunset": 1583412640
    },
    "timezone": 19800,
    "id": 1259425,
    "name": "Puducherry",
    "cod": 200

}



No comments:

Post a Comment

Popular Posts