codecov-io: # [Codecov](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=h1) Report
> Merging [#9](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=desc) into [master](https://codecov.io/gh/allisson/python-simple-rest-client/commit/2b7a0b84f9ba93ab0223da50b28365f61433e73c?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9/graphs/tree.svg?src=pr&token=YsspZn4Uwl&width=650&height=150)](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master     #9   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           6      6           
  Lines         184    188    +4     
=====================================
+ Hits          184    188    +4
```


| [Impacted Files](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [simple\_rest\_client/request.py](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9/diff?src=pr&el=tree#diff-c2ltcGxlX3Jlc3RfY2xpZW50L3JlcXVlc3QucHk=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=footer). Last update [2b7a0b8...c862d35](https://codecov.io/gh/allisson/python-simple-rest-client/pull/9?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/.gitignore b/.gitignore
index 95ea3fd..ed03ab1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -39,7 +39,7 @@ htmlcov/
 .tox/
 .coverage
 .coverage.*
-.cache
+.pytest_cache
 nosetests.xml
 coverage.xml
 *,cover
diff --git a/CHANGES.rst b/CHANGES.rst
index 13d44c2..229f54b 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,6 +1,11 @@
 Changelog
 ---------
 
+0.5.2
+~~~~~
+
+* Fix JSONDecodeError when processing empty server responses (thanks @zmbbb).
+
 0.5.1
 ~~~~~
 
diff --git a/simple_rest_client/request.py b/simple_rest_client/request.py
index b412489..05713a9 100644
--- a/simple_rest_client/request.py
+++ b/simple_rest_client/request.py
@@ -3,7 +3,7 @@ import logging
 import async_timeout
 from json_encoder import json
 
-from .decorators import handle_request_error, handle_async_request_error
+from .decorators import handle_async_request_error, handle_request_error
 from .models import Response
 
 logger = logging.getLogger(__name__)
@@ -26,7 +26,9 @@ def make_request(session, request):
     if 'text' in content_type:
         body = client_response.text
     elif 'json' in content_type:
-        body = json.loads(client_response.text)
+        body = client_response.text
+        if body:
+            body = json.loads(body)
     else:
         body = client_response.content
 
@@ -58,7 +60,9 @@ async def make_async_request(session, request):
             if 'text' in content_type:
                 body = await client_response.text()
             elif 'json' in content_type:
-                body = json.loads(await client_response.text())
+                body = await client_response.text()
+                if body:
+                    body = json.loads(body)
             else:
                 body = await client_response.read()
 

json.decoder.JSONDecodeError when processing empty server responses
@zmbbb report:

Hey, really like your lib and the elegant interface.

I encountered an exception (see below) when I received an empty response from the server, in this case a "204 NO CONTENT". The lib forwarded the empty response to JSON for decoding which raised the exception. As a quick fix, I added a check whether client_response.text is True. This works for me, for empty and non-empty responses.

The exception:
[...]
File "/usr/local/lib/python3.5/dist-packages/simple_rest_client/resource.py", line 107, in action_method
return make_request(self.session, request)
File "/usr/local/lib/python3.5/dist-packages/simple_rest_client/decorators.py", line 39, in wrapper
response = f(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/simple_rest_client/request.py", line 30, in make_request
body = json.loads(client_response.text)
File "/usr/local/lib/python3.5/dist-packages/json_encoder/json/init.py", line 229, in loads
**kw
File "/usr/lib/python3.5/json/init.py", line 332, in loads
return cls(**kw).decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

diff --git a/tests/test_resource.py b/tests/test_resource.py
index 5f8fc0b..f3a70bd 100644
--- a/tests/test_resource.py
+++ b/tests/test_resource.py
@@ -83,24 +83,25 @@ def test_resource_actions(url, method, status, action, args, kwargs, reqres_reso
     assert response.body == {'success': True}
 
 
-@pytest.mark.parametrize('content_type,response_body', [
-    ('application/json', {'success': True}),
-    ('text/plain', '{"success": true}'),
-    ('application/octet-stream', b'{"success": true}'),
+@pytest.mark.parametrize('content_type,response_body,expected_response_body', [
+    ('application/json', '{"success": true}', {'success': True}),
+    ('application/json', '', ''),
+    ('text/plain', '{"success": true}', '{"success": true}'),
+    ('application/octet-stream', '{"success": true}', b'{"success": true}'),
 ])
 @responses.activate
-def test_resource_response_body(content_type, response_body, reqres_resource):
+def test_resource_response_body(content_type, response_body, expected_response_body, reqres_resource):
     url = 'https://reqres.in/api/users'
     responses.add(
         responses.GET,
         url,
-        body=b'{"success": true}',
+        body=response_body,
         status=200,
         content_type=content_type
     )
 
     response = reqres_resource.list()
-    assert response.body == response_body
+    assert response.body == expected_response_body
 
 
 @pytest.mark.asyncio
@@ -124,14 +125,15 @@ async def test_async_resource_actions(url, method, status, action, args, kwargs,
 
 
 @pytest.mark.asyncio
-@pytest.mark.parametrize('content_type,response_body', [
-    ('application/json', {'success': True}),
-    ('text/plain', '{"success": true}'),
-    ('application/octet-stream', b'{"success": true}'),
+@pytest.mark.parametrize('content_type,response_body,expected_response_body', [
+    ('application/json', '{"success": true}', {'success': True}),
+    ('application/json', '', ''),
+    ('text/plain', '{"success": true}', '{"success": true}'),
+    ('application/octet-stream', '{"success": true}', b'{"success": true}'),
 ])
-async def test_asyncresource_response_body(content_type, response_body, reqres_async_resource):
+async def test_asyncresource_response_body(content_type, response_body, expected_response_body, reqres_async_resource):
     url = 'https://reqres.in/api/users'
     with aioresponses() as mock_response:
-        mock_response.get(url, status=200, body=b'{"success": true}', headers={'Content-Type': content_type})
+        mock_response.get(url, status=200, body=response_body, headers={'Content-Type': content_type})
         response = await reqres_async_resource.list()
-    assert response.body == response_body
+    assert response.body == expected_response_body

pip install -e .[dev]
parse_log_pytest
requirements.txt
aioresponses asynctest codecov flake8 pytest pytest-asyncio pytest-cov responses Sphinx twine wheel
apt-get update apt-get install -y gcc
3.6
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
aiohttp==3.8.6
aioresponses==0.7.6
aiosignal==1.2.0
alabaster==0.7.13
async-timeout==4.0.2
asynctest==0.13.0
attrs==22.2.0
Babel==2.11.0
bleach==4.1.0
certifi==2021.5.30
cffi==1.15.1
charset-normalizer==2.0.12
codecov==2.1.13
colorama==0.4.5
coverage==6.2
cryptography==40.0.2
docutils==0.17.1
flake8==5.0.4
frozenlist==1.2.0
idna==3.10
idna-ssl==1.1.0
imagesize==1.4.1
importlib-metadata==4.2.0
importlib-resources==5.4.0
iniconfig==1.1.1
jeepney==0.7.1
Jinja2==3.0.3
json-encoder==0.4.4
keyring==23.4.1
MarkupSafe==2.0.1
mccabe==0.7.0
multidict==5.2.0
packaging==21.3
pkginfo==1.10.0
pluggy==1.0.0
py==1.11.0
pycodestyle==2.9.1
pycparser==2.21
pyflakes==2.5.0
Pygments==2.14.0
pyparsing==3.1.4
pytest==7.0.1
pytest-asyncio==0.16.0
pytest-cov==4.0.0
python-status==1.0.1
pytz==2025.2
readme-renderer==34.0
requests==2.27.1
requests-toolbelt==1.0.0
responses==0.17.0
rfc3986==1.5.0
SecretStorage==3.3.3
-e git+https://github.com/allisson/python-simple-rest-client.git@2b7a0b84f9ba93ab0223da50b28365f61433e73c#egg=simple_rest_client
singledispatch==3.7.0
six==1.17.0
snowballstemmer==2.2.0
Sphinx==4.3.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==1.2.3
tqdm==4.64.1
twine==3.8.0
typing_extensions==4.1.1
urllib3==1.26.20
webencodings==0.5.1
yarl==1.7.2
zipp==3.6.0

name: python-simple-rest-client
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - aiohttp==3.8.6
      - aioresponses==0.7.6
      - aiosignal==1.2.0
      - alabaster==0.7.13
      - async-timeout==4.0.2
      - asynctest==0.13.0
      - attrs==22.2.0
      - babel==2.11.0
      - bleach==4.1.0
      - cffi==1.15.1
      - charset-normalizer==2.0.12
      - codecov==2.1.13
      - colorama==0.4.5
      - coverage==6.2
      - cryptography==40.0.2
      - docutils==0.17.1
      - flake8==5.0.4
      - frozenlist==1.2.0
      - idna==3.10
      - idna-ssl==1.1.0
      - imagesize==1.4.1
      - importlib-metadata==4.2.0
      - importlib-resources==5.4.0
      - iniconfig==1.1.1
      - jeepney==0.7.1
      - jinja2==3.0.3
      - json-encoder==0.4.4
      - keyring==23.4.1
      - markupsafe==2.0.1
      - mccabe==0.7.0
      - multidict==5.2.0
      - packaging==21.3
      - pkginfo==1.10.0
      - pluggy==1.0.0
      - py==1.11.0
      - pycodestyle==2.9.1
      - pycparser==2.21
      - pyflakes==2.5.0
      - pygments==2.14.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-asyncio==0.16.0
      - pytest-cov==4.0.0
      - python-status==1.0.1
      - pytz==2025.2
      - readme-renderer==34.0
      - requests==2.27.1
      - requests-toolbelt==1.0.0
      - responses==0.17.0
      - rfc3986==1.5.0
      - secretstorage==3.3.3
      - singledispatch==3.7.0
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==4.3.2
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==1.2.3
      - tqdm==4.64.1
      - twine==3.8.0
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - webencodings==0.5.1
      - yarl==1.7.2
      - zipp==3.6.0
prefix: /opt/conda/envs/python-simple-rest-client

tests/test_resource.py::test_resource_response_body[application/json--] tests/test_resource.py::test_asyncresource_response_body[application/json--]
tests/test_resource.py::test_base_resource_actions tests/test_resource.py::test_base_resource_get_action_full_url tests/test_resource.py::test_base_resource_get_action_full_url_with_append_slash tests/test_resource.py::test_base_resource_get_action_full_url_with_action_not_found tests/test_resource.py::test_base_resource_get_action_full_url_with_action_url_match_error tests/test_resource.py::test_custom_resource_actions tests/test_resource.py::test_custom_resource_get_action_full_url tests/test_resource.py::test_resource_actions[https://reqres.in/api/users-GET-200-list-None-kwargs0] tests/test_resource.py::test_resource_actions[https://reqres.in/api/users-POST-201-create-None-kwargs1] tests/test_resource.py::test_resource_actions[https://reqres.in/api/users/2-GET-200-retrieve-2-kwargs2] tests/test_resource.py::test_resource_actions[https://reqres.in/api/users/2-PUT-200-update-2-kwargs3] tests/test_resource.py::test_resource_actions[https://reqres.in/api/users/2-PATCH-200-partial_update-2-kwargs4] tests/test_resource.py::test_resource_actions[https://reqres.in/api/users/2-DELETE-204-destroy-2-kwargs5] tests/test_resource.py::test_resource_response_body[application/json-{"success": tests/test_resource.py::test_resource_response_body[text/plain-{"success": tests/test_resource.py::test_resource_response_body[application/octet-stream-{"success": tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users-GET-200-list-None-kwargs0] tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users-POST-201-create-None-kwargs1] tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users/2-GET-200-retrieve-2-kwargs2] tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users/2-PUT-200-update-2-kwargs3] tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users/2-PATCH-200-partial_update-2-kwargs4] tests/test_resource.py::test_async_resource_actions[https://reqres.in/api/users/2-DELETE-204-destroy-2-kwargs5] tests/test_resource.py::test_asyncresource_response_body[application/json-{"success": tests/test_resource.py::test_asyncresource_response_body[text/plain-{"success": tests/test_resource.py::test_asyncresource_response_body[application/octet-stream-{"success":
.gitignore simple_rest_client/request.py CHANGES.rst
.gitignore simple_rest_client/request.py CHANGES.rst