Can't send POST request to an old website

Question:

I tried different methods for so long to submit a POST request to a form using Python requests but no luck. I’m trying to send '3513' to form’s text field which has the name TextTrnNo and then get the information from the website.

One of my approaches that fails:

import requests

payload = {'TextTrnNo': '3513'}


r = requests.post('https://appiris.infofer.ro/mytrainro.aspx', data=payload)

print(r.text)

The output I get:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html ) throw new Error('ASP.NET Ajax client-side framework failed to load.');
    //]]>
    </script>

    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="86BE64DB" />
        <p class="style28">
            <img id="Image5" src="Img/icons/flagRO.jpg" />
    &nbsp;CNCF "CFR SA" - IRIS Informatii tren</p>
        <p class="style28">
            <span class="style1">
            <span id="LblError">Servicii IRIS disponibile</span>
            </span>
                </p>
        <div>
            
            
            
            
            <div id="UpdateProgress1" style="display:none;">
        
                    <span class="style26">
                    <img id="Image3" src="Images/Animate/pleasewait_7.gif" height="20" width="22" />
                    &nbsp;</span><span class="style1">Asteptati...</span>
                
    </div>
        <div id="UpdatePanel1">
        
                <span class="style26"><span class="style1">
                <img id="Image4" src="Images/tren.jpg" height="110" width="200" />
                <br />
                </span>
                <span class="style30">Program circulatie</span>
                <span class="style26"><span class="style17">
                <span id="Label1" class="style10">13.01.2019   17:00 -14.01.2019   17:00</span>
                <table class="style29">
                    <tr>
                        <td valign="bottom">
                            
                        </td>
                        <td>
                            <span class="style26">
                            
                            
                            </span>
                        </td>
                        <td>
                            <span class="style26">
                            
                            </span>
                        </td>
                        <td>
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                </table>
                Numar tren (fără alte caractere suplimentare)</span>
                <input name="TextTrnNo" type="text" maxlength="15" id="TextTrnNo" title="Introduceti numai numărul trenului, fără alte caractere suplimentare" />
                &nbsp;<input type="submit" name="Button1" value="Informatii tren" id="Button1" style="font-family: Tahoma; font-size: small; font-weight: 700; color: #006699; background-color: #FFFFFF" />
                <span id="Lblx" style="font-family: Tahoma; font-size: small; color: #CC0000"><b><font size="5"></font></b></span>
                <br />
                <br>
                <div>

        </div>
                
                &nbsp;
                <div>

        </div>
                <div>

        </div>
                <br />
                <br />
                
                
                
            
    </div>
        </div>
        </form>
    </body>
    </html>

Headers from Google Chrome:

    Request URL: https://appiris.infofer.ro/ScriptResource.axd?d=JNg0fH_zydENJ7nFcOtvc6yqY25aZ8vD-Ng7zdFvgOejMomgDzr5SQOfJxd86HrHIAnJdwgVKGKwlCLMquTcg919u2vCLYJz5V0xHnmOduOfc5VgeUHRZPW3jweNO1vqw9JZLWTSVL3z295y_EUsB350MMAhbUa41ssYQzHWget11mAeRCd_ANONdJnWqlOV0&t=5854e822
    Request Method: GET
    Status Code: 200  (from memory cache)
    Remote Address: 193.230.156.188:443
    Referrer Policy: no-referrer-when-downgrade
    cache-control: public
    content-encoding: gzip
    content-length: 55951
    content-type: application/x-javascript
    date: Sun, 13 Jan 2019 17:04:19 GMT
    expires: Sun, 12 Jan 2020 22:49:44 GMT
    last-modified: Sat, 12 Jan 2019 22:49:44 GMT
    server: Microsoft-IIS/6.0
    status: 200
    x-powered-by: ASP.NET
    Provisional headers are shown
    d: JNg0fH_zydENJ7nFcOtvc6yqY25aZ8vD-Ng7zdFvgOejMomgDzr5SQOfJxd86HrHIAnJdwgVKGKwlCLMquTcg919u2vCLYJz5V0xHnmOduOfc5VgeUHRZPW3jweNO1vqw9JZLWTSVL3z295y_EUsB350MMAhbUa41ssYQzHWget11mAeRCd_ANONdJnWqlOV0
    t: 5854e822
Asked By: Ax M

||

Answers:

Your code is correct as in its sending the post request with the data you want. However, whilst looking at network requests sent to the site, I noticed that there’s a couple fields you’re missing that prevent the post request from being parsed by the site. These fields being:

  • Button1
  • __VIEWSTATE

They can be completely blank, they just have to be part of the post request, thus making your full payload variable:

payload = {'TextTrnNo': '3513', 'Button1': '', '__VIEWSTATE': ''}

This returns a different output that includes some sort of table.

Answered By: Chloe