Add JsonResponse (#6174)

This commit is contained in:
Jalil SA 2023-12-21 03:36:21 -06:00 committed by GitHub
parent b498f1376d
commit d25cfe5315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 6 deletions

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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",

View File

@ -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: