Http put 401 error

I started this new thread in the hopes that someone will be able to help - I had been posting my issues and investigations in the existing thread User Preferences - http PUT 401 but nobody but steevej-1495 was replying (thank you). The issue is that other than the USER & SESSION API writes to the database, all other API writes/deletes are failing - COMMENT, LOGOUT APIs so far. Screen shots below:

Using the same credentials to connect to Atlas, I have tested the Comment insert and Session delete db commands via Jupyter and they work fine.

Hope someone has some ideas as to why these 401 errors are occurring.

I only have 8 days to complete this - hope someone can help!

Hi @Darwin_LeMay

I’ve replied also in the other thread, essentially a 401 is an unusual error. Can you confirm that the user has been correctly configured with the read/write all databases permission firstly?

Kindest regards,

Hi Eoin - thanks for responding - am getting a bit desperate!

Yes - using the same credentials, I can perform the exact same deletes etc. using Jupyter. The user is set as an admin. Yesterday I also tried installing an older version of Anaconda, as the Python version is now 3.8, but the screen shot of the installer was 3.6. I uninstalled & installed Anaconda3 v5.2 (same as screenshot) but it did not make any difference.

It’s odd, as I can insert a user and a session, but can’t insert user preferences or a comment. Logout fails to delete the session record, and also get same error when trying to edit/delete a comment.

I am using Atlas, and am on Windows 10. Also using Compass and the same credentials I can insert/delete on the db.

Hi @Darwin_LeMay

Firstly to remove it, can you take a screenshot to clarify the user privileges in Atlas and attach it.

In terms of the Jupyter notebook, is that running on the same directory and code or is it referencing another version of the code?

Can you point me to the corresponding exercise - chapter and lesson so that I can ensure I can try the exact steps you are doing and for again completeness can you add a number list with these?

I realise this is quite a lot of information, however given the nature and the lack of a solution at this point so far, I think closing out any potential aspects (the DB user privileges) and then getting your exact sequence will more rapidly allow me to ensure I am actually working on your problem and not a potential similar but unrelated issue.

Thanks and kindest regards,

Hi - here’s the requested info:

User permissions:

Ini file: (thought it might help)
SECRET_KEY = thisismysecretkey
MFLIX_DB_URI = mongodb+srv://
MFLIX_NS = sample_mflix

SECRET_KEY = thisismysecretkey
MFLIX_DB_URI = mongodb+srv://
MFLIX_NS = sample_mflix

Running on the same code. I start it in the “mflix-python” directory, same as where my conda env is and python is started from.

Course Chapters - these are ones I cannot complete:
Chapter 2, Ticket: User Preferences, Ticket: Create/Update Comments, Ticket: Delete Comments

These tests have in common that they need a valid session. I suspect that your create session is not working correctly. It is possible that the unit test for session creation work if your session has error that the unit test does not detect but that is really needed for the rest. Sometime it is simply a type mismatch, like an ObjectId processed as a string or a number.

How can we test that?

Here’s my code for the “login_user” function:


In terms of set, you only need to ‘set’ the jwt.

What about the user preference code? We are not supposed to share but you suffered enough already.

Thanks @steevej-1495! Here it is:

It looks like in some of the code you are using user_id as the key while in some other you are using email as the key. Which one is used for createUser?

Couldn’t find a function called “createUser” - there is “add_user” - but there is no key, as it creates the user record…

Session record key is called “user_id”, which is actually the email, and the prefs code also uses the email as the key or the “user” record.

Also noticed that none of my user records have a “preference” field - does that matter? I’m assuming the “Update_one” creates the field if it doesn’t exists - correct?

Please share the code.

Here it is. This works, it does insert new user record.


I do not see anything that would cause what you get.

Check if you have an index on email in the users collection and an index on user_id in the sessions collection.

Yes to both. Sigh.

Can I just post the code via Discord that I have done for the 3 tickets and if it’s correct is there a way to mark them completed? I’ve been struggling with this for a few weeks now, running out of time.

I have completed the rest of the course, including the final exam. 0 failures on any of the other completed tickets & final.

So - if I just fail these 3 tickets so that everything is completed, will this guarantee a pass? I understand the code, don’t want to waste anymore of your and my time on this if we don’t need to.

If everything is completed you should be able to see your final grade right away.

Those 3 tickets - User Preferences, Create/Update Comments, Delete Comments are not done, as I cannot get the test to succeed, so the course will not give me a final grade.