diff --git a/klarnacheckout/connector.py b/klarnacheckout/connector.py index e7067e3..af615ae 100644 --- a/klarnacheckout/connector.py +++ b/klarnacheckout/connector.py @@ -82,7 +82,11 @@ def apply(self, method, resource, options=None): if method == 'POST': req.add_header('Content-Type', content_type) data = options.get('data') or resource.marshal() - req.data = json.dumps(data).encode('utf-8') + if isinstance(data, (list, tuple, dict)): + # data which is iterable must be serialized + req.data = json.dumps(data).encode('utf-8') + else: + req.data = data.encode('utf-8') return self.handle_response(resource, self.opener.open(req)) diff --git a/tests/unit/test_order.py b/tests/unit/test_order.py index 1a83104..7fb4690 100644 --- a/tests/unit/test_order.py +++ b/tests/unit/test_order.py @@ -97,6 +97,15 @@ def test_create(self): self._connector.apply.assert_called_once_with( "POST", self._order, {"url": location, "data": data}) + def test_create_json(self): + location = "http://stub" + self._order.base_uri = location + data = '{"foo": "boo"}' + self._order.create(data) + + self._connector.apply.assert_called_once_with( + "POST", self._order, {"url": location, "data": data}) + def test_fetch(self): location = "http://stub" self._order.location = location