You cannot access the REST API without a valid session ID. HTTP 401 code suggests your login failed or your session was timed out.
It is a good practice to check the results of the API call and re-login if your session was timed out. If your login was unsuccessful, an HTTP 401 result is the expected behavior.
I didn't figure out if you have encountered an unexpected behavior through your tests (meaning you successfully logged in and couldn't use the API). If you did please comment below and try to provide a detailed scenario.
Hope this helps,