How to perform HTTPS Calls in Pycurl?

1.7k views Asked by At

I want to login into a website sending POST request using `pycurl' on a HTTPS page. I did the following:

import pycurl, urllib
curl = pycurl.Curl()
curl.setopt(pycurl.COOKIEFILE, "")
post = "_username=something&_password=somethingelse&_submit=Login"
curl.setopt(pycurl.URL, "https://www.example.com/login_check")
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.POSTFIELDS, post)
curl.perform()

But I wasn't able to login. May be that was because my code made http call rather. Although I am able to perform the login sending 'POST' request with the same parameters in `POSTMAN'

So seems like login is right but I am doing something wrong implementing the same in python. Pl guide.

1

There are 1 answers

2
Paul Trott On BEST ANSWER

According to the pycURL documentation, your post data needs to be key value pairs and url encoded:

post_data = {'field': 'value'}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
c.setopt(c.POSTFIELDS, postfields)