Permalink
Browse files

make sure ocntent type is defined

  • Loading branch information...
1 parent 10b53e3 commit 4def0a6334ef989fcc9f28788904fdb41e3d31ba @benoitc benoitc committed Nov 28, 2009
Showing with 23 additions and 15 deletions.
  1. +23 −15 couchapp/couchdbresource.py
@@ -189,24 +189,32 @@ def request(self, method, path=None, payload=None, headers=None, **params):
headers = headers or {}
headers.update(self._headers.copy())
+
size = headers.get('Content-Length', None)
- if isinstance(payload, file):
- try:
- payload.flush()
- except IOError:
- pass
- size = int(os.fstat(payload.fileno())[6])
- elif isinstance(payload, types.StringTypes):
- payload = to_bytestring(payload)
- size = len(payload)
- elif not hasattr(payload, 'read') and not isinstance(payload, basestring):
- payload = json.dumps(payload).encode('utf-8')
- headers.setdefault('Content-Type', 'application/json')
- size = len(payload)
+ if payload:
+ if isinstance(payload, file):
+ try:
+ payload.flush()
+ except IOError:
+ pass
+ size = int(os.fstat(payload.fileno())[6])
+ elif isinstance(payload, types.StringTypes):
+ payload = to_bytestring(payload)
+ size = len(payload)
+ elif not hasattr(payload, 'read') and not isinstance(payload, basestring):
+ payload = json.dumps(payload).encode('utf-8')
+ headers.setdefault('Content-Type', 'application/json')
+ size = len(payload)
- if payload is not None and size is not None:
- headers.setdefault('Content-Length', size)
+ if payload is not None and size is not None:
+ headers.setdefault('Content-Length', size)
+
+ if 'Content-Type' not in headers:
+ type_ = None
+ if hasattr(payload, 'name'):
+ type_ = mimetypes.guess_type(payload.name)[0]
+ headers['Content-Type'] = type_ and type_ or 'application/octet-stream'
headers = _normalize_headers(headers)
uri = url_parser(make_uri(self.url, path, **encode_params(params)))

0 comments on commit 4def0a6

Please sign in to comment.