Home > OS >  Send POST request in Python returning response as 500
Send POST request in Python returning response as 500

Time:07-17

I'm trying to scrape a website in which a form is getting submitted in POST request to fetch data.

Below is the code I am executing:

from bs4 import BeautifulSoup
import requests

headers = {
    'Content-Type':'application/x-www-form-urlencoded',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
url = 'https://ipindiaservices.gov.in/PublicSearch/PublicationSearch/Search'

with requests.Session() as s:
    response = s.get('https://ipindiaservices.gov.in/PublicSearch/PublicationSearch/', headers=headers)
    
    soup = BeautifulSoup(response.content, 'html.parser')
    hidden = soup.find_all("input", {'type':'hidden'})
    params = {x["name"]: x["value"] for x in hidden}
    params['TextField6'] = 'ltd'
    response = s.post(url, headers=headers, params=params)
    soup = BeautifulSoup(response.content, 'html.parser')
    print(response)

I am getting 500 as the value of response for the post request. The get request is working fine and giving me response as 200. Not sure what is going wrong in the post request. While submitting the form I am populating Please Enter Applicant Name field as ltd. Remaining fields are having default values. Below is the url in which the form is getting submitted from: https://ipindiaservices.gov.in/PublicSearch/PublicationSearch

I am updating all input fields TextField1 - TextField15 to spaces except TextField6 where I am passing value as ltd. Also I am setting the two hidden fields Published and Granted with the default value.

The two check box fields are also having same name as the 2 hidden fields - Published and Granted. Not sure if this is what causing the problem

<input checked="checked" data-val="true" data-val-required="The Published field is required." id="Published" name="Published" title="Published" type="checkbox" value="true"/>
<input name="Published" type="hidden" value="false"/>
<input data-val="true" data-val-required="The Granted field is required." id="Granted" name="Granted" title="Granted" type="checkbox" value="true"/> 
<input name="Granted" type="hidden" value="false"/>

CodePudding user response:

A 500 response code means there is an uncaught exception on the server, nothing really you can do about. You could try playing with some of the values being sent in the request and maybe you could get a 2xx response code (or at least not a 500)

CodePudding user response:

My bad, there was a typo in my code. response = s.post(url, headers=headers, params=params) should be response = s.post(url, headers=headers, data=params)

The issue got resolved. Please close this one.

  • Related