scrapy / scrapy
1
"""
2
This module implements the JsonRequest class which is a more convenient class
3
(than Request) to generate JSON Requests.
4

5
See documentation in docs/topics/request-response.rst
6
"""
7

8 7
import copy
9 7
import json
10 7
import warnings
11

12 7
from scrapy.http.request import Request
13 7
from scrapy.utils.deprecate import create_deprecated_class
14

15

16 7
class JsonRequest(Request):
17 7
    def __init__(self, *args, **kwargs):
18 7
        dumps_kwargs = copy.deepcopy(kwargs.pop('dumps_kwargs', {}))
19 7
        dumps_kwargs.setdefault('sort_keys', True)
20 7
        self._dumps_kwargs = dumps_kwargs
21

22 7
        body_passed = kwargs.get('body', None) is not None
23 7
        data = kwargs.pop('data', None)
24 7
        data_passed = data is not None
25

26 7
        if body_passed and data_passed:
27 7
            warnings.warn('Both body and data passed. data will be ignored')
28

29 7
        elif not body_passed and data_passed:
30 7
            kwargs['body'] = self._dumps(data)
31

32 7
            if 'method' not in kwargs:
33 7
                kwargs['method'] = 'POST'
34

35 7
        super().__init__(*args, **kwargs)
36 7
        self.headers.setdefault('Content-Type', 'application/json')
37 7
        self.headers.setdefault('Accept', 'application/json, text/javascript, */*; q=0.01')
38

39 7
    def replace(self, *args, **kwargs):
40 7
        body_passed = kwargs.get('body', None) is not None
41 7
        data = kwargs.pop('data', None)
42 7
        data_passed = data is not None
43

44 7
        if body_passed and data_passed:
45 0
            warnings.warn('Both body and data passed. data will be ignored')
46

47 7
        elif not body_passed and data_passed:
48 7
            kwargs['body'] = self._dumps(data)
49

50 7
        return super().replace(*args, **kwargs)
51

52 7
    def _dumps(self, data):
53
        """Convert to JSON """
54 7
        return json.dumps(data, **self._dumps_kwargs)
55

56

57 7
JSONRequest = create_deprecated_class("JSONRequest", JsonRequest)

Read our documentation on viewing source code .

Loading