My code to vote on a comment is not working correctly. It returns an http 500 error.
I have a Python program that logs in with a user and which is supposed to automatically upvote a comment.
The code I have is the following:
<code>from bs4 import BeautifulSoup
import requests
login_url = "https://xxxxxxxxxxx/auth/login"
login_url_post = "https://xxxxxxxxxxx/auth/login_check"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Accept-Language': 'es,es-ES;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
}
session = requests.Session()
response_get = session.get(login_url)
soup = BeautifulSoup(response_get.text, "html.parser")
# Find field "value" from "_csrf_token"
csrf_token = soup.find("input", {"name": "_csrf_token"})["value"]
# Payload
payload = {
"_csrf_token": csrf_token,
"_username": "email",
"_password": "password",
}
response_post = session.post(login_url_post, data=payload, headers=headers)
# Now I have logged in as a user (verified)
###### vote comment after having logged in ##################
# The Link comment is another but the url for vote is another direction
url_vote = "https://xxxxxxxxxxx/api/1.0/comment/2187750/vote"
payload_vote = {
"comment_id": 2187750,
"value": 1
}
# Requests Post for vote comment
response_post_vote = session.post(url_vote, data=payload_vote, headers=headers)
print(response_post_vote)
</code>
<code>from bs4 import BeautifulSoup
import requests
login_url = "https://xxxxxxxxxxx/auth/login"
login_url_post = "https://xxxxxxxxxxx/auth/login_check"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Accept-Language': 'es,es-ES;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
}
session = requests.Session()
response_get = session.get(login_url)
soup = BeautifulSoup(response_get.text, "html.parser")
# Find field "value" from "_csrf_token"
csrf_token = soup.find("input", {"name": "_csrf_token"})["value"]
# Payload
payload = {
"_csrf_token": csrf_token,
"_username": "email",
"_password": "password",
}
response_post = session.post(login_url_post, data=payload, headers=headers)
# Now I have logged in as a user (verified)
###### vote comment after having logged in ##################
# The Link comment is another but the url for vote is another direction
url_vote = "https://xxxxxxxxxxx/api/1.0/comment/2187750/vote"
payload_vote = {
"comment_id": 2187750,
"value": 1
}
# Requests Post for vote comment
response_post_vote = session.post(url_vote, data=payload_vote, headers=headers)
print(response_post_vote)
</code>
from bs4 import BeautifulSoup
import requests
login_url = "https://xxxxxxxxxxx/auth/login"
login_url_post = "https://xxxxxxxxxxx/auth/login_check"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
'Accept-Language': 'es,es-ES;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6'
}
session = requests.Session()
response_get = session.get(login_url)
soup = BeautifulSoup(response_get.text, "html.parser")
# Find field "value" from "_csrf_token"
csrf_token = soup.find("input", {"name": "_csrf_token"})["value"]
# Payload
payload = {
"_csrf_token": csrf_token,
"_username": "email",
"_password": "password",
}
response_post = session.post(login_url_post, data=payload, headers=headers)
# Now I have logged in as a user (verified)
###### vote comment after having logged in ##################
# The Link comment is another but the url for vote is another direction
url_vote = "https://xxxxxxxxxxx/api/1.0/comment/2187750/vote"
payload_vote = {
"comment_id": 2187750,
"value": 1
}
# Requests Post for vote comment
response_post_vote = session.post(url_vote, data=payload_vote, headers=headers)
print(response_post_vote)
The last “print” returns an http 500 error.
Here I show the data when I vote from the web browser and the responses that it tells me from the web inspector.
New contributor
Vladi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1