Practice Exercise: Basic Computer Networking API Testing with Linux Curl
Introduction
Welcome to the Basic Computer Networking lab designed to equip future Site Reliability Engineers (SREs) with the essential skills of testing APIs using the Linux Curl command. In this lab, you will learn how to make API requests, handle responses, and troubleshoot common issues using Curl. API testing is a fundamental skill for ensuring the reliability and functionality of web services.
Prerequisites
- A Linux server with Curl installed.
- Basic familiarity with the Linux command line.
Scenario
As a future SRE, you will often need to test and interact with various APIs to monitor and maintain the health of your services. You will use a Linux server as your client and the publicly accessible API server at https://httpbin.org
for practice.
Tasks
Task 1: Making a GET Request
- Open a terminal on your Linux server.
- Use the "curl" command to make a GET request to the API endpoint:
https://httpbin.org/get
. - Observe and note the response, including the status code, headers, and content.
curl https://httpbin.org/get
Task 2: Making a GET Request to Another Endpoint
- Make a GET request to
https://httpbin.org/anything
. httpbin.org/anything will look at the request you made, parse it, and echo back to you what you requested. Curl's default is to make a GET request. - Observe and note the response.
curl https://httpbin.org/anything
Task 3: Making a POST Request
- Use Curl to make a POST request to
https://httpbin.org/anything
. - Capture and inspect the response.
curl -X POST https://httpbin.org/anything
Task 4: Making a GET Request with Query Parameters
- Make a GET request to
https://httpbin.org/anything
, but this time add some query parameters (set value=panda). - Analyze the response for the updated query parameters.
curl "https://httpbin.org/anything?value=panda"
Task 5: Requesting a robots.txt File
- Request Google's robots.txt file at
www.google.com/robots.txt
. - Capture and inspect the response.
curl www.google.com/robots.txt
Task 6: Setting a Custom User-Agent Header
- Make a GET request to
https://httpbin.org/anything
. - Set the header
User-Agent: elephant
. - Capture and inspect the response.
curl -H "User-Agent: elephant" https://httpbin.org/anything
Task 7: Making a DELETE Request
- Use Curl to make a DELETE request to
https://httpbin.org/anything
. - Capture and inspect the response.
curl -X DELETE https://httpbin.org/anything
Task 8: Requesting Headers
- Make a GET request to
https://httpbin.org/anything
and also get the response headers. - Capture and inspect the headers in the response.
curl -I https://httpbin.org/anything
Task 9: Making a POST Request with JSON Body
- Make a POST request to
https://httpbin.org/anything
with the JSON body{"value": "panda"}
. - Capture and inspect the response.
curl -X POST -d '{"value": "panda"}' https://httpbin.org/anything
Task 10: Setting Content-Type Header for POST Request
- Make the same POST request as the previous exercise, but set the
Content-Type
header toapplication/json
(because POST requests need to have a content type that matches their body). - Look at the
json
field in the response to see the difference from the previous one.
curl -X POST -H "Content-Type: application/json" -d '{"value": "panda"}' https://httpbin.org/anything
Sample Troubleshooting Command
Simulate a network issue by disconnecting your internet temporarily. Use curl
to make a request during the downtime. Troubleshoot the error message received and suggest potential solutions.