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': '[email protected]',
...         '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?

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.