Capturing the Request
For this tutorial, we will be using Chrome - please note this can be done in other browsers as well, but the steps may be different. To capture a network request in Chrome, you can use the Chrome DevTools:
- Open the DevTools by pressing
F12
orCtrl + Shift + I
on Windows/Linux orCmd + Opt + I
on macOS - Go to the Network tab (at the top) and reload the page
After reloading, you should see a list of all the requests made by the browser. If your request occurs after an action, you will see the request appear after you perform the action.
If you don't see any requests, make sure that the filter is set to "All".
Look through the list of requests to find the one you want to replay. You can click on the request to see more details about it.
When you have found the request you want to replay, right-click on it (on the left side of the Network tab - not the preview) and select "Copy" > "Copy as cURL (bash)". This will copy the request as a cURL command to your clipboard.
Replaying the Request
To replay this request, we first need to convert the cURL command to Python code. After this, we can then use the requests library to execute the request.
Converting the cURL command to Python
To convert the cURL command to Python, we can use curlconverter.com. Paste the cURL command into the box labelled "curl command" and make sure "Python" is selected underneath. This will generate the Python code for you.
Copy the output for the next step, it will look something like this:
import requests
headers = {
'Accept': 'text/plain',
'Connection': 'keep-alive',
'Origin': 'https://example.com',
'Referer': 'https://example.com/',
# More headers like User-Agent, etc.
}
response = requests.get('https://example.com/my/request', cookies=cookies, headers=headers)
Executing the Python Code
You would have noticed that the Python code generated by curlconverter.com imports the requests
library. If you don't have this installed, you can install it using pip install requests
.
Create a new Python file and paste the code generated by curlconverter.com into it. To see what the response looks like, you can add the following code to the end of the file:
print(response.status_code)
print(response.text) # If JSON is being returned, you can use `response.json()` instead
Running the file should print the status code and response body.
Modify the Request
Now that we have the request working, you can modify it to suit your needs. For example, you can change the URL to point to a different endpoint, change the headers to use different values or send a different body.
This is a great way to test APIs or to automate tasks that you would normally do in the browser.