[Answered ]-Django Rest Framework – use serializers to send json with requests library

2πŸ‘

βœ…

It’s feasible, if you see the need. Although, the JSON encoding is done in the Response object, which is a full HTTPResponse subclass, so you would need to encode your own data:

import json
import requests

my_objects = Reservation.objects.all()
serializer = ReservationSerializer(data=my_objects, many=True)
if serializer.is_valid():
  # now you do your encoding:
  encoded_data = json.dumps(serializer.data)
  response = requests.post(your_url, 
                           headers={'Content-Type': 'application/json'},
                           data=encoded_data)
πŸ‘€fixmycode

Leave a comment