'Send POST request in Python

I'm trying to scrape a website in which I need to send a POST request to a form to query data. Here is the code I'm using.

import requests

headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"}

with requests.Session() as s:
    
    r = s.get('https://data.rabbu.com', headers=headers)
    
    soup = BeautifulSoup(r.text, 'html.parser')
    hidden = soup.find_all("input", {'type':'hidden'})
    payload = {x["name"]: x["value"] for x in hidden}
    payload['search'] = '16101 Tampa Street, Brooksville FL 34604'
    payload['bedrooms'] = '2'
    
    r = s.post('https://data.rabbu.com/e', headers=headers, data=payload)
    soup = BeautifulSoup(r.text, 'html.parser')
    print(soup.text)

But I'm unable to send properly the POST request because I'm getting the following error message:

"The change you wanted was rejected (422)"

I tried to use the "json" argument instead of "data" - to no avail.

Do you have any idea how I can bypass this issue? Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source