0

I have the following curl that successfully logs me into a website. It returns a 302 and a cookie:

curl \
--verbose \
--request POST \
--data '__EVENTTARGET=&body%3Ax%3AtxtLogin=kingkong%40mailinator.com&body%3Ax%3AbtnLogin=&crypted_pass=736015615f9e251692a6a4aa8a7baa14' \
"https://emma.maryland.gov/page.aspx/en/usr/login"

Unfortunately, I have to insert the real username & encrypted password of the account. otherwise this curl it won't work. However, this is a completely dummy account with NO private data in it. So please don't get mad at me.

I want to convert it to a Python3 code using requests.post(). So I made this code:

>>> requests.post(
...     url='https://emma.maryland.gov/page.aspx/en/usr/login',
...     data={
...         '__EVENTTARGET': '',
...         'body:x:txtLogin': 'kingkong@mailinator.com',
...         'body:x:btnLogin': '',
...         'crypted_pass': '736015615f9e251692a6a4aa8a7baa14'
...     }
... )
<Response [200]>

But the response I get from the Python3 code (200) doesn't match the response I get from the Curl (302). This means that the target server senses a difference between the two requests.

How can I convert the curl to Python3 that sends the exact same underlying HTTP request?

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272

1 Answers1

2

Your requests code is actually smarter than the cURL command.

HTTP 302 - is a redirect, cURL didn't follow it and gave you the first response it got. You can make cURL follow the redirect with -L: Is there a way to follow redirects with command line cURL?

The requests code followed the redirect and gave you the final response, which happened to be a HTTP 200.

Try your curl command with -L and see if you get HTTP 200 or not.

Alternatively, you can ask requests to not follow redirects with the allow_redirects=False option: Is there an easy way to request a URL in python and NOT follow redirects?

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Curl -L doesn't follow. It still gives a 302. But adding the `allow_redirects=False` solves my problem by making my python return 302. That's what I want. I don't want to follow the redirect. THANKS! – Saqib Ali Oct 04 '19 at 19:23