Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Getting 200 ok response but username and password fields are empty its again redirecting to login page

Getting 200 ok response but username and password fields are empty its again redirecting to login page can you help me on this?

import requests
from bs4 import BeautifulSoup

header = {
    'User-Agent': 'Mozilla/5.0   (Windows NT 10.0; Win64; x64)  AppleWebKit/537.36 (KHTML, like  Gecko) Chrome/118.0.0.0 Safari/537.36   Edg/118.0.2088.61'
}

Payload = {
    "utf8": "✓",
    "spree_user[email]":     "yavixe4893@tutoreve.com",
    "spree_user[password]":   "Qwerty@123",
    "spree_user[remember_me]": "0",
    "commit": "Login",
}

with requests.Session() as ses:
    Url = "https://mightynest.com"
    Req = ses.get(Url)
    soup = BeautifulSoup(Req.content,   'html5lib')
    print(Req.content)
    Payload['authenticity_token'] = soup.find('meta', {'name': 'csrf-token'})['content']
    res = ses.post('http://mightynest.com/login', data=Payload, headers=header)
    print(res.content)

Note: Username/password are correct but still facing same issue.

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Disclaimer: Just guessing here, I haven't taken this class.

Make sure that you're extracting the CSRF token correctly. The website's HTML structure might have changed, or there may be multiple CSRF tokens. You're currently looking for a meta tag with the name csrf-token shouldn't you be looking for authenticity_token? Is tthis still valid for the website?

The URL you're posting to is using http instead of https, and it's possible that the endpoint has changed or requires a secure connection. Double-check the form's action URL on the login page to ensure it's correct.