I'm trying to POST
to a web page that sets a JSESSIONID
HTTP cookie. When trying to set this cookie automatically using the HTTPCookieProcessor
, the page gives a blank response and when I print out the request headers, Cookie
is missing.
Code:
@contextmanager
def get_opener():
global proxies
proxy = proxies.get()
try:
yield request.build_opener(
request.HTTPCookieProcessor(),
request.ProxyHandler(proxy)
)
finally:
proxies.put(proxy)
def download(data, destination, tries=0):
with get_opener() as opener:
first_response = opener.open(request.Request('https://example.org/page.html'))
print('First response info:')
print(first_response.info())
req = request.Request(Config.DOWNLOAD_URL, data=data, method='POST')
response = opener.open(req)
print('Second response:')
print(response.info())
print(response.read())
print(req.headers)
Output:
First response info:
Set-Cookie: JSESSIONID=04CBF51EE096C4AD382D7D556CDF356D; Path=/; Secure; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Transfer-Encoding: chunked
Vary: Accept-Encoding
Date: Wed, 24 Jul 2019 05:37:05 GMT
Connection: close
Second response:
Content-Length: 0
Date: Wed, 24 Jul 2019 05:37:06 GMT
Connection: close
b''
{} # note no 'Cookie' header
I was under the impression that the cookies would automatically be set: