Shikha Gupta
Shikha Gupta

Reputation: 83

How to work with cookies with httplib in python

I am sending soap request using httplib but when I send it I am getting the following issue:

INFO:root:Response:<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

I am setting already headers below:

auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
headers = {"Content-type": "text/xml;charset=utf-8","SOAPAction":"\"\"","Authorization": "Basic %s" % auth}

Instead of python if I send by SoapUI then it seems when request is sent by SoapUI from server some cookie is being sent and then soapUI resends it with cookie.

Upvotes: 1

Views: 274

Answers (1)

Shikha Gupta
Shikha Gupta

Reputation: 83

Instead of httplib, I used the urllib2 module. I need not set a cookie, I changed the code to use Digest type authentication, instead of Basic authentication:

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None,uri=uri,user=username,passwd=password)
authHandler=urllib2.HTTPDigestAuthHandler(password_mgr)
opener = urllib2.build_opener(authHandler)
response=opener.open(webServiceUrl2,data)
res=response.read()

Upvotes: 1

Related Questions