Skip to main content
Tweeted twitter.com/StackCodeReview/status/1617265854030258177
Became Hot Network Question
Remove thanks - say thanks instead by voting. See https://codereview.stackexchange.com/help/why-vote
Source Link

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice).

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://example.net/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice).

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://example.net/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice).

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://example.net/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()
Use standard example domain.
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

I am using the requestsrequests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a forfor loop (which seems bad practice).

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://apiexample.somenet/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice)

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://api.some/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice).

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://example.net/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!

Source Link

Python: Handling different HTTP status codes using the requests library

I am using the requests library to make requests to an API. However, I am not sure how to handle different status codes. I am particularly interested in handling status code 500, as that is part of my task.

1: Is a try/except block like I have an acceptable way of handling error codes?

2: How should I handle 500 status code? Would retrying 5 times once per second, then exiting be acceptable? If so, I don't know how to implement that without wrapping the whole function in a for loop (which seems bad practice)

Here is my function that makes HTTP requests:

def make_request(type, endpoint, h, data=None):
    try:
        url = f'https://api.some/not-important/{endpoint}'
        if type == 'GET':
            r = requests.get(url, headers=h)
        elif type == 'POST':
            r = requests.post(url, headers=h, json=data)
        # r = requests.get('https://httpstat.us/500')
        r.raise_for_status()
    except requests.exceptions.HTTPError as e:
        if e.response.status_code == 500:
            # 500 logic here. retry?
        else:
            print(f'An error occurred: {e}\n{e.response.status_code}')
            sys.exit(1)

    else:
        return r.json()

Thank you!