Skip to content

How To Use Postman Openweathermap

API Testing Blog

How to Use Postman to Test OpenWeatherMap API

OpenWeatherMap is a popular weather data API that provides real-time weather information for various locations worldwide. Postman is a powerful tool for API testing, allowing you to send requests, analyze responses, and automate tests. This guide will walk you through the steps of using Postman to test OpenWeatherMap API, providing practical examples and sample code.

Getting Started: Your OpenWeatherMap API Key

Before you start, you’ll need to obtain an API key from OpenWeatherMap. This key is essential for authenticating your requests to retrieve data.

  1. Visit the OpenWeatherMap Website: Navigate to https://openweathermap.org/.
  2. Sign Up or Log In: If you don’t have an account, register for a free one.
  3. Get Your API Key: After logging in, you’ll find your API key on your account’s dashboard. Store this key securely and keep it handy.

Setting Up Postman for OpenWeatherMap API

  1. Launch Postman: Open the Postman application on your computer.
  2. Create a New Request: Click on the “New” button and select “Request” to create a new request.
  3. Set the Request Method: For most OpenWeatherMap API calls, you’ll use the GET method.
  4. Enter the API Endpoint: The basis of your request URL will be the OpenWeatherMap API endpoint. For example, to get the current weather for a specific city: https://api.openweathermap.org/data/2.5/weather

How to Use Postman for Getting Current Weather Data

This example shows you how to get the current weather forecast by city.

  1. Compose the Request URL:

    https://api.openweathermap.org/data/2.5/weather?q=London&appid=YOUR_API_KEY
    • Replace YOUR_API_KEY with your actual OpenWeatherMap API key.
    • Replace London with the city you want to check the weather for.
  2. Add the API Key as a Header (Optional): You can also add the API key as a request header. This is recommended for more sensitive API calls.

    • Click on the Headers tab.
    • Enter x-api-key as the header key and your API key as the value.
  3. Send the Request: Click on the “Send” button to execute the request.

How to Use Postman for Getting Historical Weather Data

You can use the history endpoint to retrieve historical weather data.

  1. Compose the Request URL:

    https://api.openweathermap.org/data/2.5/history/city?q=London&type=day&start=1684012800&end=1684099200&appid=YOUR_API_KEY
    • Replace YOUR_API_KEY with your actual OpenWeatherMap API key.
    • Replace London with the city you want to check the weather for.
    • Replace 1684012800 and 1684099200 with the desired start and end timestamps. You can use a timestamp converter to easily find these values.
    • You can set the type parameter to hour for hourly data or day for daily data.

How to Use Postman for Getting Forecast Data

You can use the forecast endpoint to get a 5-day/3-hour forecast for a specific location.

  1. Compose the Request URL:

    https://api.openweathermap.org/data/2.5/forecast?q=London&appid=YOUR_API_KEY
    • Replace YOUR_API_KEY with your actual OpenWeatherMap API key.
    • Replace London with the city you want to check the forecast for.

How to Use Postman for Testing and Validating API Responses

Postman offers powerful features to test and validate API responses:

  1. Response Body: Examine the response body to ensure it contains the expected weather data. This can be done by inspecting the raw JSON response or using the Pretty view for a more readable format.
  2. Response Status Code: Verify that the response status code is 200 OK, indicating a successful request.
  3. Assertions: Use Postman’s powerful assertion capabilities to validate the structure and values of the JSON response. Assertions allow you to write automated tests to check specific data points in your API response. An example of an assertion would be pm.expect(response.json().main.temp).to.be.above(20).

Example Postman Collections for OpenWeatherMap API Testing

By using Postman Collections, you can organize multiple requests related to OpenWeatherMap API into a single, reusable group. This makes it easier to manage and execute your API tests.

  1. Create a New Collection: In Postman, click on the “Collections” tab and then “Create Collection”. Give your collection a descriptive name like “OpenWeatherMap API Tests”.
  2. Add Requests to the Collection: Add the individual requests created above to your collection.
  3. Organize Tests: You can add specific tests to each request within the collection by using the “Tests” tab in Postman.

Conclusion

Postman provides a comprehensive and user-friendly platform for testing OpenWeatherMap API. By following these steps and using the provided examples, you can effectively send requests, analyze responses, and validate the accuracy of the weather data returned by the API. Postman’s ability to create reusable collections and apply assertions makes it a powerful tool for both manual and automated API testing.

API Testing Blog