json_request.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. This module implements the JsonRequest class which is a more convenient class
  3. (than Request) to generate JSON Requests.
  4. See documentation in docs/topics/request-response.rst
  5. """
  6. import copy
  7. import json
  8. import warnings
  9. from scrapy.http.request import Request
  10. from scrapy.utils.deprecate import create_deprecated_class
  11. class JsonRequest(Request):
  12. def __init__(self, *args, **kwargs):
  13. dumps_kwargs = copy.deepcopy(kwargs.pop('dumps_kwargs', {}))
  14. dumps_kwargs.setdefault('sort_keys', True)
  15. self._dumps_kwargs = dumps_kwargs
  16. body_passed = kwargs.get('body', None) is not None
  17. data = kwargs.pop('data', None)
  18. data_passed = data is not None
  19. if body_passed and data_passed:
  20. warnings.warn('Both body and data passed. data will be ignored')
  21. elif not body_passed and data_passed:
  22. kwargs['body'] = self._dumps(data)
  23. if 'method' not in kwargs:
  24. kwargs['method'] = 'POST'
  25. super(JsonRequest, self).__init__(*args, **kwargs)
  26. self.headers.setdefault('Content-Type', 'application/json')
  27. self.headers.setdefault('Accept', 'application/json, text/javascript, */*; q=0.01')
  28. def replace(self, *args, **kwargs):
  29. body_passed = kwargs.get('body', None) is not None
  30. data = kwargs.pop('data', None)
  31. data_passed = data is not None
  32. if body_passed and data_passed:
  33. warnings.warn('Both body and data passed. data will be ignored')
  34. elif not body_passed and data_passed:
  35. kwargs['body'] = self._dumps(data)
  36. return super(JsonRequest, self).replace(*args, **kwargs)
  37. def _dumps(self, data):
  38. """Convert to JSON """
  39. return json.dumps(data, **self._dumps_kwargs)
  40. JSONRequest = create_deprecated_class("JSONRequest", JsonRequest)