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
- Public API: An API that is available to the public and can be accessed without any special permissions or authentication.
- API Endpoint: A specific URL where an API can be accessed.
- HTTP Methods: Common methods include GET, POST, PUT, DELETE, which are used to interact with the API.
- 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
- 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
.
- 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.
- Set Up Postman
- Open Postman and create a new request.
- Enter the API endpoint URL in the request URL field.
- 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
orunits
.
- Send the Request
- Click the "Send" button in Postman to execute the request.
- Observe the response returned by the API.
- 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:
- Open Postman and create a new GET request.
- Enter the URL:
https://randomuser.me/api/
- Send the Request: Click "Send".
- 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.
- Create a GET request in Postman.
- Set the URL to
https://api.agify.io?name=michael
. - 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.
Postman and API Testing Course
Module 1: Introduction to APIs and Postman
Module 2: Basic API Testing with Postman
- Creating Your First Request
- Understanding Request and Response
- Using Postman Collections
- Environment Variables in Postman
Module 3: Intermediate API Testing Techniques
Module 4: Advanced Postman Features
- Automating Tests with Newman
- Continuous Integration with Postman
- Mock Servers in Postman
- Advanced Scripting Techniques
Module 5: API Testing Best Practices
- Designing Effective Test Cases
- Handling Authentication
- Error Handling and Debugging
- Performance Testing with Postman