Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Add JsonResponse (#6174)

Jalil SAd25cfe5315c9
diff --git a/docs/topics/request-response.rst b/docs/topics/request-response.rst
index 8edf710bc..9d64eee45 100644
--- a/docs/topics/request-response.rst
+++ b/docs/topics/request-response.rst
@@ -1357,3 +1357,13 @@ XmlResponse objects
line. See :attr:`TextResponse.encoding`.
.. _bug in lxml: https://bugs.launchpad.net/lxml/+bug/1665241
+
+JsonResponse objects
+--------------------
+
+.. class:: JsonResponse(url[, ...])
+
+ The :class:`JsonResponse` class is a subclass of :class:`TextResponse`
+ that is used when the response has a `JSON MIME type
+ <https://mimesniff.spec.whatwg.org/#json-mime-type>`_ in its `Content-Type`
+ header.
diff --git a/scrapy/http/__init__.py b/scrapy/http/__init__.py
index ac3946302..d0b726bad 100644
--- a/scrapy/http/__init__.py
+++ b/scrapy/http/__init__.py
@@ -12,5 +12,6 @@ from scrapy.http.request.json_request import JsonRequest
from scrapy.http.request.rpc import XmlRpcRequest
from scrapy.http.response import Response
from scrapy.http.response.html import HtmlResponse
+from scrapy.http.response.json import JsonResponse
from scrapy.http.response.text import TextResponse
from scrapy.http.response.xml import XmlResponse
diff --git a/scrapy/http/response/json.py b/scrapy/http/response/json.py
new file mode 100644
index 000000000..219691094
--- /dev/null
+++ b/scrapy/http/response/json.py
@@ -0,0 +1,12 @@
+"""
+This module implements the JsonResponse class that is used when the response
+has a JSON MIME type in its Content-Type header.
+
+See documentation in docs/topics/request-response.rst
+"""
+
+from scrapy.http.response.text import TextResponse
+
+
+class JsonResponse(TextResponse):
+ pass
diff --git a/scrapy/responsetypes.py b/scrapy/responsetypes.py
index 9e411d4aa..0d127d851 100644
--- a/scrapy/responsetypes.py
+++ b/scrapy/responsetypes.py
@@ -21,9 +21,9 @@ class ResponseTypes:
"application/xhtml+xml": "scrapy.http.HtmlResponse",
"application/vnd.wap.xhtml+xml": "scrapy.http.HtmlResponse",
"application/xml": "scrapy.http.XmlResponse",
- "application/json": "scrapy.http.TextResponse",
- "application/x-json": "scrapy.http.TextResponse",
- "application/json-amazonui-streaming": "scrapy.http.TextResponse",
+ "application/json": "scrapy.http.JsonResponse",
+ "application/x-json": "scrapy.http.JsonResponse",
+ "application/json-amazonui-streaming": "scrapy.http.JsonResponse",
"application/javascript": "scrapy.http.TextResponse",
"application/x-javascript": "scrapy.http.TextResponse",
"text/xml": "scrapy.http.XmlResponse",
diff --git a/tests/test_responsetypes.py b/tests/test_responsetypes.py
index 6e1ed82f0..713a83d52 100644
--- a/tests/test_responsetypes.py
+++ b/tests/test_responsetypes.py
@@ -1,6 +1,13 @@
import unittest
-from scrapy.http import Headers, HtmlResponse, Response, TextResponse, XmlResponse
+from scrapy.http import (
+ Headers,
+ HtmlResponse,
+ JsonResponse,
+ Response,
+ TextResponse,
+ XmlResponse,
+)
from scrapy.responsetypes import responsetypes
@@ -40,8 +47,9 @@ class ResponseTypesTest(unittest.TestCase):
("application/vnd.wap.xhtml+xml; charset=utf-8", HtmlResponse),
("application/xml; charset=UTF-8", XmlResponse),
("application/octet-stream", Response),
- ("application/x-json; encoding=UTF8;charset=UTF-8", TextResponse),
- ("application/json-amazonui-streaming;charset=UTF-8", TextResponse),
+ ("application/json; encoding=UTF8;charset=UTF-8", JsonResponse),
+ ("application/x-json; encoding=UTF8;charset=UTF-8", JsonResponse),
+ ("application/json-amazonui-streaming;charset=UTF-8", JsonResponse),
(b"application/x-download; filename=\x80dummy.txt", Response),
]
for source, cls in mappings: