Hello, I want that when I open a web page it detects if there is an error in the load in the http request and if there is any error it makes a series of steps. I can’t do it, how can I do it?
Thank you very much
Hello RubenLA,
To detect HTTP response status codes when making a request to a web page, you can use a programming language such as Python or JavaScript and send an HTTP request to the website using a library such as requests in Python or fetch in JavaScript.
In Python, you can use the following code to check the status code of a website:
import requests
response = requests.get('http://www.example.com')
if response.status_code == 200:
print('Website loaded successfully')
else:
print(f'Error loading website: {response.status_code}')
# Add your series of steps here for handling the error
In JavaScript, you can use the following code to check the status code of a website:
fetch('http://www.example.com')
.then(response => {
if (response.ok) {
console.log('Website loaded successfully');
} else {
console.log(`Error loading website: ${response.status}`);
// Add your series of steps here for handling the error
}
})
.catch(error => {
console.error('Error loading website:', error);
// Add your series of steps here for handling the error
});
These code snippets will check the HTTP status code of the website’s response and handle any errors accordingly.
I hope it will help you.