In this section, we will explore how to test public APIs using Postman. Public APIs are accessible over the internet and can be used by anyone to retrieve or send data. Testing these APIs is crucial to ensure they function as expected and provide the correct data.

Key Concepts

  1. Public API: An API that is available to the public and can be accessed without any special permissions or authentication.
  2. API Endpoint: A specific URL where an API can be accessed.
  3. HTTP Methods: Common methods include GET, POST, PUT, DELETE, which are used to interact with the API.
  4. Response Codes: Standard HTTP status codes that indicate the result of the API request (e.g., 200 for success, 404 for not found).

Steps to Test Public APIs

  1. Identify the API Endpoint

  • Determine the base URL and the specific endpoint you want to test.
  • Example: For a weather API, the endpoint might be https://api.weather.com/v3/wx/conditions/current.

  1. Choose the HTTP Method

  • Decide which HTTP method to use based on the action you want to perform.
  • Example: Use GET to retrieve data from the API.

  1. Set Up Postman

  • Open Postman and create a new request.
  • Enter the API endpoint URL in the request URL field.

  1. Configure Request Parameters

  • Add any necessary query parameters or headers required by the API.
  • Example: For a weather API, you might need to include parameters like location or units.

  1. Send the Request

  • Click the "Send" button in Postman to execute the request.
  • Observe the response returned by the API.

  1. Analyze the Response

  • Check the status code to ensure the request was successful.
  • Review the response body to verify the data returned by the API.

Practical Example

Let's test a public API that provides random user data.

Example: Random User API

Endpoint: https://randomuser.me/api/

HTTP Method: GET

Steps:

  1. Open Postman and create a new GET request.
  2. Enter the URL: https://randomuser.me/api/
  3. Send the Request: Click "Send".
  4. Analyze the Response:
    • Check the status code (should be 200 for success).
    • Review the JSON response body, which contains random user data.
{
  "results": [
    {
      "gender": "female",
      "name": {
        "title": "Ms",
        "first": "Jane",
        "last": "Doe"
      },
      "email": "[email protected]",
      "dob": {
        "date": "1985-05-15T09:44:18.674Z",
        "age": 36
      }
    }
  ],
  "info": {
    "seed": "abc",
    "results": 1,
    "page": 1,
    "version": "1.3"
  }
}

Explanation:

  • Status Code: 200 indicates the request was successful.
  • Response Body: Contains user details such as name, email, and date of birth.

Exercise

Task: Test the public API https://api.agify.io which predicts the age of a person based on their name.

  1. Create a GET request in Postman.
  2. Set the URL to https://api.agify.io?name=michael.
  3. Send the Request and analyze the response.

Solution:

  • Expected Status Code: 200
  • Expected Response:
    {
      "name": "michael",
      "age": 69,
      "count": 12345
    }
    

Common Mistakes

  • Incorrect URL: Ensure the endpoint URL is correct.
  • Missing Parameters: Some APIs require specific parameters; check the API documentation.
  • Ignoring Status Codes: Always check the status code to understand the result of your request.

Conclusion

Testing public APIs with Postman is a straightforward process that involves setting up requests, sending them, and analyzing the responses. By practicing with different public APIs, you can enhance your skills in API testing and better understand how APIs work. In the next section, we will explore testing private APIs, which often require authentication and additional security measures.

© Copyright 2024. All rights reserved