Getting http request status codes

What I’m trying to do:

Hi,

I have some server code that makes http POST requests to an API. I am on the free plan so cannot use requests and so am using anvil.http.request

My http request is working ok but I cannot work out how to read the http status code returned by the API (200 if successful). I have read the documentation and the only thing I can find that is related to this is the anvil.http.HttpError command.

Is there any way of just seeing the http status code rather than just error codes?

Below is the code that I am using to make the http POST request. This works fine but I want to be able to check the status code to see confirm it is successful before moving on to the next operation. This is very easy to do using requests when using normal Python.

Code Sample:

createRoomResponse = anvil.http.request(url='https://webexapis.com/v1/rooms',
                    method="POST",
                    data={'title': f'{displayName} Test Space'},
                    headers= {'Authorization': 'Bearer ' + webexaccesstoken,'Content-type': 'application/json;charset=utf-8'}
                    )

From the lack of responses I am guessing that this is not supported?
I use type of functionality often in non-Anvil apps where I can use the requests module.
Does anyone else think that this feature would be useful in Anvil or is there some reason that it is not required?

With anvil.http you can access the status for http request fails

Anvil Docs | anvil.http

try:
    createRoomResponse = anvil.http.request(url='https://webexapis.com/v1/rooms',
                    method="POST",
                    data={'title': f'{displayName} Test Space'},
                    headers= {'Authorization': 'Bearer ' + webexaccesstoken,'Content-type': 'application/json;charset=utf-8'}
                    )
except anvil.http.HttpError as e:
    print(e.status, e.content)

if the status code was between 200 and 299 then the request was successful and returns the body of the request.

related docs:

Anvil Docs | Making HTTP requests

2 Likes