Skip to content
Myr

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

1 file+41 added−14 removed

modified:requests/core.py+41 added-14 removed
32 unmodified lines
33 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 lines
64 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 pass6970 except Exception:71 raise RequestException72
20 unmodified lines
93 94 elif self.method.lower() == 'post':95 if (not self.sent) or anyway:96 try:101 pass9798 req = urllib2.Request(self.url)99100 if self.headers:101 r.headers = self.headers102103 # 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.data108109110 if self.auth:111112 # create a password manager113 password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()114115 # 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)120121 # use the opener to fetch a URL122 resp = opener.open(req)123 else:124 resp = urllib2.urlopen(req)125126 self.response.status_code = resp.code127 self.response.headers = resp.info().dict128 self.response.content = resp.read()129 130 success = True131132 except Exception:
55 unmodified lines
188 189 return r.response190191165def head(url, params={}, headers={}, auth=None):192def 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.response206207181def post(url, params={}, headers={}, auth=None):208def post(url, data={}, headers={}, auth=None):209 """Sends a POST request. Returns :class:`Response` object.210 """211 r = Request()185 212213 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 lines
282 """A valid URL is required to make a request."""283 284class InvalidMethod(RequestException):285 """An inappropriate method was attempted."""258