Basic POST in place.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent d4c1ecc · Browse files · 1 file·+41 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+41 added−14 removed
- other:+41 added−14 removed
Provenance
- Commit
44aa680- Tree
21557f3- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
d4c1ecc
1 file changed+41 added−14 removed
modified:requests/core.py+41 added-14 removed
32 unmodified lines33 34 _METHODS = ('get', 'head', 'put', 'post', 'delete')35 36 def __init__(self):37+ self.url = None38 self.headers = dict()39 self.method = None40 self.params = {}40- self.data = None41+ self.data = {}42 self.response = Response()43 self.auth = None44 self.sent = False45 18 unmodified lines64 65 if self.method.lower() == 'get':66 if (not self.sent) or anyway:67 try:67- r = urllib.urlopen('http://kennethreitz.com')68- self.response.headers = r.headers.dict69- self.response.status_code = r.code70- self.response.content = r.read()71- 72- success = True68+ pass69 70 except Exception:71 raise RequestException72 20 unmodified lines93 94 elif self.method.lower() == 'post':95 if (not self.sent) or anyway:96 try:101- pass97+98+ req = urllib2.Request(self.url)99+100+ if self.headers:101+ r.headers = self.headers102+103+ # url encode form data if it's a dict104+ if isinstance(self.data, dict):105+ req.data = urllib.urlencode(self.data)106+ else:107+ req.data = self.data108+109+110+ if self.auth:111+112+ # create a password manager113+ password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()114+115+ # Add the username and password.116+ # If we knew the realm, we could use it instead of ``None``.117+ password_mgr.add_password(None, self.url, self.auth.username, self.auth.password)118+ handler = urllib2.HTTPBasicAuthHandler(password_mgr)119+ opener = urllib2.build_opener(handler)120+121+ # use the opener to fetch a URL122+ resp = opener.open(req)123+ else:124+ resp = urllib2.urlopen(req)125+126+ self.response.status_code = resp.code127+ self.response.headers = resp.info().dict128+ self.response.content = resp.read()129 130 success = True131 132 except Exception:55 unmodified lines188 189 return r.response190 191 165-def head(url, params={}, headers={}, auth=None):192+def head(url, data={}, headers={}, auth=None):193 """Sends a HEAD request. Returns :class:`Response` object.194 """195 r = Request()196 197 r.method = 'HEAD'198 # return response object172- 199+ r.data = data200 r.headers = headers201 r.auth = _detect_auth(url, auth)202 203 r.send()204 205 return r.response206 207 181-def post(url, params={}, headers={}, auth=None):208+def post(url, data={}, headers={}, auth=None):209 """Sends a POST request. Returns :class:`Response` object.210 """211 r = Request()185- 212+213+ r.url = url214 r.method = 'POST'187- # return response object215+ r.data = data216 217 r.headers = headers218 r.auth = _detect_auth(url, auth)219 62 unmodified lines282 """A valid URL is required to make a request."""283 284 class InvalidMethod(RequestException):285 """An inappropriate method was attempted."""258-