Skip to content

Commit cc10d35

Browse files
Modify URL validation method and error handling (#159)
feat: optimize URL validation to use HEAD requests by default Update validate_url_200 to default get_method to requests.head for improved efficiency. Implement fallback to GET for specific status codes (403, 405, 501) to handle servers that reject HEAD requests. It is worth noting that this change does not resolve 429 Too Many Requests errors. Because HEAD requests are still requests, they count toward your total traffic quota on the target server. By design, this function honors the server's rate-limiting policy, it does not bypass it. If you encounter a 429 error, the function will correctly raise a ValidationError as expected, signaling that the application needs to implement backoff or throttling logic rather than attempting to "fix" the request method.
1 parent 2b9890d commit cc10d35

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

courses/validators/custom_url_validators.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ def get_error_message(status_code, url):
2525
def validate_url_200(
2626
url, get_method=None, code=None, params=None
2727
):
28+
# Since the
2829
if get_method is None:
29-
get_method = requests.get
30+
get_method = requests.head
3031

3132
try:
3233
response = get_method(url)
34+
35+
if response.status_code in [403, 405, 501]:
36+
response = requests.get(url)
37+
3338
status_code = response.status_code
39+
3440
if status_code == 200:
3541
return
42+
3643
error_message = get_error_message(status_code, url)
3744
raise ValidationError(error_message, code=code, params=params)
3845
except requests.exceptions.RequestException as e:

0 commit comments

Comments
 (0)