From 40c5d1592a462e9136950383cea55e4a8acbcf4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Zakraj=C5=A1ek?= Date: Sun, 15 Jul 2012 15:03:20 +0200 Subject: [PATCH 1/4] send content-type only if there is data, fix post when location is not full url --- slumber/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/slumber/__init__.py b/slumber/__init__.py index d6e2cf2..bea32ce 100644 --- a/slumber/__init__.py +++ b/slumber/__init__.py @@ -98,7 +98,12 @@ def _request(self, method, data=None, params=None): if self._store["append_slash"] and not url.endswith("/"): url = url + "/" - resp = self._store["session"].request(method, url, data=data, params=params, headers={"content-type": s.get_content_type(), "accept": s.get_content_type()}) + headers = {"accept": s.get_content_type()} + + if data is not None: + headers["content-type"] = s.get_content_type() + + resp = self._store["session"].request(method, url, data=data, params=params, headers=headers) if 400 <= resp.status_code <= 499: raise exceptions.HttpClientError("Client Error %s: %s" % (resp.status_code, url), response=resp, content=resp.content) @@ -126,7 +131,11 @@ def post(self, data, **kwargs): if 200 <= resp.status_code <= 299: if resp.status_code == 201: # @@@ Hacky, see description in __call__ - resource_obj = self(url_override=resp.headers["location"]) + location = resp.headers["location"] + if location.startswith("/"): + resource_obj = self(id=location) + else: + resource_obj = self(url_override=location) return resource_obj.get(params=kwargs) else: return resp.content From 63aae2bc10388fef5a75c54819081713483b89d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Zakraj=C5=A1ek?= Date: Sat, 2 Mar 2013 19:57:02 +0100 Subject: [PATCH 2/4] Fixed POST response content, updated gitignore --- .gitignore | 2 ++ slumber/__init__.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 229ecfa..e493a09 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ docs/_build/* +*.pyc +*~ diff --git a/slumber/__init__.py b/slumber/__init__.py index bea32ce..6fcb9d8 100644 --- a/slumber/__init__.py +++ b/slumber/__init__.py @@ -138,7 +138,9 @@ def post(self, data, **kwargs): resource_obj = self(url_override=location) return resource_obj.get(params=kwargs) else: - return resp.content + s = self.get_serializer() + + return s.loads(resp.content) else: # @@@ Need to be Some sort of Error Here or Something return From 7789bd3e1a8b5d9a78b146de20594a0bf15a6122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Zakraj=C5=A1ek?= Date: Thu, 21 Mar 2013 02:30:17 +0100 Subject: [PATCH 3/4] Fixed POST response serializer --- slumber/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/slumber/__init__.py b/slumber/__init__.py index 6fcb9d8..e2161f6 100644 --- a/slumber/__init__.py +++ b/slumber/__init__.py @@ -138,9 +138,8 @@ def post(self, data, **kwargs): resource_obj = self(url_override=location) return resource_obj.get(params=kwargs) else: - s = self.get_serializer() - - return s.loads(resp.content) + if resp.content: + return self.get_serializer().loads(resp.content) else: # @@@ Need to be Some sort of Error Here or Something return From 3f0807e61ae59e1c8d2c6a73eea87844862d06f8 Mon Sep 17 00:00:00 2001 From: overlordtm Date: Mon, 12 Aug 2013 15:51:26 +0200 Subject: [PATCH 4/4] Make slumber not crash on 201 response without Location header If we get 201 response with no location header, treat is as 200 --- slumber/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slumber/__init__.py b/slumber/__init__.py index e2161f6..cff2ba7 100644 --- a/slumber/__init__.py +++ b/slumber/__init__.py @@ -129,9 +129,9 @@ def post(self, data, **kwargs): resp = self._request("POST", data=s.dumps(data), params=kwargs) if 200 <= resp.status_code <= 299: - if resp.status_code == 201: + location = resp.headers.get("location") + if resp.status_code == 201 and location is not None: # @@@ Hacky, see description in __call__ - location = resp.headers["location"] if location.startswith("/"): resource_obj = self(id=location) else: