Django Rest Framework way to ingest json data? - python

Hey guys I'm trying to ingest some data from a third party API when a DRF endpoint is called.
I can easily build the get request using urllib2 and deserialize the json, but is there some way to use django serializers and do it the drf way? Or am I just overthinking this?
import json, urllib2
class IngestThirdPartyView(APIView):
def post(self, request):
data = request.data
url = 'https://third_party.com/media/{}'.format(data['item_id]')
response = urllib.urlopen(url)
# Handle item somehow
item = json.load(response)
Any suggestions for a more DRF friendly way to handle this are very much welcome!
Cheers

From your example code,
import requests
class IngestThirdPartyView():
def post(self, request):
data = request.data
url = 'https://third_party.com/media/{}'.format(data['item_id]')
item = requests.get(url).json() # this will retrieve json response
# then you could deserialize the above json and validate it or something
# as described here in docs http://www.django-rest-framework.org/api-guide/serializers/#deserializing-objects
# Handle item somehow

Related

How can I bundle my JSON API data into one dictionary?

I'm trying to package my API data into one GET request simply using standard libraries python.
class GetData(APIView):
def get(self, request, *args, **kwargs):
urls = [url_1,
url_2,
url_3,
url_4
]
data_bundle = []
for x in urls:
response = requests.get(x, headers={'Content-Type': 'application/json'}).json()
data_bundle.append(response)
return Response(data_bundle, status=status.HTTP_200_OK)
The return response has to be JSON data, I'm trying to get it to work but it seems like the response data seems to be overiding eachother? How can I properly create a JSON dictionary of dictionaries.
I've tried switching data_bundle to an empty dictionary instead of a list. However that just caused an error saying:
ValueError: dictionary update sequence element #0 has length 8; 2 is required
Is there a simple way to accomplish this that I'm missing?
Thank you for the help.
class GetData(APIView):
def get(self, request, *args, **kwargs):
urls = [url_1,
url_2,
url_3,
url_4
]
data_bundle = []
for x in urls:
response = requests.get(x, headers={'Content-Type': 'application/json'}).json()
data_bundle.append(response)
return Response(data_bundle, status=status.HTTP_200_OK)
maybe this will help dont use return in body of for cycle. And data will not override each other

Json parsing django rest framework

I want to parse incoming POST data in django views.py file
POST data:
{
"number" : "17386372",
"data" : ["banana","apple","grapes" ]
}
Here is how I tried to read above incoming data with request
views.py
class Fruits(APIView):
def post(self, request, format=None):
if request.method == "POST":
number = request.data.get('number')
fruits_data = json.loads(request.body)
if number not in [None, '', ' ']:
try:
response = {"return": "OK","data":fruits_data['data']}
return Response(response)
except:
return Response({"return": "NOT OK"})
else:
return Response({"return": "NOT OK"})
else:
return Response({"return": "NOT OK"})
ERROR:
You cannot access body after reading from request's data stream
The Django json parser does this already for you:
from rest_framework import parsers
class Fruits(APIView):
parser_classes = (parsers.JSONParser,)
def post(self, request, format=None):
number = request.data['number']
fruits = request.data['data']
If the Content-Type of your http request is already set properly to application/json you do not even need to specify the parser.
request.data and request.body are the two mechanisms, which reads the raw http request and construct data in a format, that is suitable to be used in python environment. Here the problem is that you are using both of them simultaneously. Thus, the inputstream of http connection is already read, by request.data call. Now request.body also tries to access the same stream, which doesn't contain now any data. Thus, it's throwing an error.
For you, I think following code will work :
fruits_data = json.loads(request.body)
number = fruits_data["number"]

How do I use django rest framework to send a file in response?

I need to send a pdf file and some other parameters in response to a get API call using django rest framework.
How can I do it?
I tried this but it gives an error <django.http.HttpResponse object at 0x7f7d32ffafd0> is not JSON serializable.
#detail_route(methods=['get'])
def fetch_report(self, request, *args, **kwargs):
short_report = open("somePdfFile", 'rb')
response = HttpResponse(FileWrapper(short_report), content_type='application/pdf')
return Response({'detail': 'this works',
'report': response})
The problem here is that you are trying to return a mix of JSON and PDF, which either isn't what you are looking for or is going to return a giant base64-encoded response. PDF is a binary format and JSON is a text format, and you can't really mix them well.
Within a DRF view you can directly return a Django response, which you already appear to be generating (the HttpResponse), and DRF will pass it through and skip the renderers. This is useful in cases like this, as it allows you to return a binary response such as an image or PDF without worrying about DRF's rendering layer causing problems.
#detail_route(methods=['get'])
def fetch_report(self, request, *args, **kwargs):
short_report = open("somePdfFile", 'rb')
response = HttpResponse(FileWrapper(short_report), content_type='application/pdf')
return response
The alternative is to encode the PDF as text, using something like base64 encoding. This will dramatically increase your response sizes, but it will allow you to use DRF's rendering layer without problems.
#detail_route(methods=['get'])
def fetch_report(self, request, *args, **kwargs):
import base64
short_report = open("somePdfFile", 'rb')
report_encoded = base64.b64encode(short_report.read())
return Response({'detail': 'this works',
'report': report_encoded})
But the route I would recommend here is to generate the PDF and store it in either your media storage, or an alternative private location, and provide a direct link to it in your response. This way you don't need to worry about the encoding issues, don't need to directly return the PDF, and don't need to worry about directly serving the PDF.
If you really need to serve binary files directly from your backend (e.g. when they're being generated dynamically) you can use a custom renderer:
from rest_framework.renderers import BaseRenderer
class BinaryFileRenderer(BaseRenderer):
media_type = 'application/octet-stream'
format = None
charset = None
render_style = 'binary'
def render(self, data, media_type=None, renderer_context=None):
return data
Then use it in your action in a viewset:
from rest_framework.decorators import action
#action(detail=True, methods=['get'], renderer_classes=(BinaryFileRenderer,))
def download(self, request, *args, **kwargs):
with open('/path/to/file.pdf', 'rb') as report:
return Response(
report.read(),
headers={'Content-Disposition': 'attachment; filename="file.pdf"'},
content_type='application/pdf')
You can use DRF-PDF project with PDFFileResponse:
from rest_framework import status
from rest_framework.views import APIView
from drf_pdf.response import PDFFileResponse
from drf_pdf.renderer import PDFRenderer
class PDFHandler(APIView):
renderer_classes = (PDFRenderer, )
def get(self, request):
return PDFFileResponse(
file_path='/path/to/file.pdf',
status=status.HTTP_200_OK
)
But, maybe you cannot respond in both formats (json and stream).
You can send pdf file as a response to any request without installing any packages.
You can do the following.
def get(request, *args, **kwargs):
# some code
# some code
with fs.open("path/to/file/Report.pdf") as pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Report.pdf"
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
return response
Now, open your postman and hit a get/post request.
Important: Before clicking send button in postman, select the option send and download.
With DRF you can write as following:
pdf_file_in_bytes = AnyModel.file.read() # file is a FileField
pdf_file_name = "PDF's Download Name"
response = Response(
headers={'Content-Disposition': f'attachment; filename={pdf_file_name}'},
content_type='application/pdf'
)
response.content = pdf_file_in_bytes
return response

How do I return JSON without using a template in Django?

This is related to this question: Django return json and html depending on client python
I have a command line Python API for a Django app. When I access the app through the API it should return JSON and with a browser it should return HTML. I can use different URLs to access the different versions but how do I render the HTML template and JSON in the views.py with just one template?
To render the HTML I would use:
return render_to_response('sample/sample.html....')
But how would I do the same for JSON without putting a JSON template? (the content-type should be application/json instead of text/html)
What would determine the JSON and HTML outputs?
So in my views.py:
if something:
return render_to_response('html_template',.....)
else:
return HttpReponse(jsondata,mimetype='application/json')
I think the issue has gotten confused regarding what you want. I imagine you're not actually trying to put the HTML in the JSON response, but rather want to alternatively return either HTML or JSON.
First, you need to understand the core difference between the two. HTML is a presentational format. It deals more with how to display data than the data itself. JSON is the opposite. It's pure data -- basically a JavaScript representation of some Python (in this case) dataset you have. It serves as merely an interchange layer, allowing you to move data from one area of your app (the view) to another area of your app (your JavaScript) which normally don't have access to each other.
With that in mind, you don't "render" JSON, and there's no templates involved. You merely convert whatever data is in play (most likely pretty much what you're passing as the context to your template) to JSON. Which can be done via either Django's JSON library (simplejson), if it's freeform data, or its serialization framework, if it's a queryset.
simplejson
from django.utils import simplejson
some_data_to_dump = {
'some_var_1': 'foo',
'some_var_2': 'bar',
}
data = simplejson.dumps(some_data_to_dump)
Serialization
from django.core import serializers
foos = Foo.objects.all()
data = serializers.serialize('json', foos)
Either way, you then pass that data into the response:
return HttpResponse(data, content_type='application/json')
[Edit] In Django 1.6 and earlier, the code to return response was
return HttpResponse(data, mimetype='application/json')
[EDIT]: simplejson was remove from django, you can use:
import json
json.dumps({"foo": "bar"})
Or you can use the django.core.serializers as described above.
In Django 1.7 this is even easier with the built-in JsonResponse.
https://docs.djangoproject.com/en/dev/ref/request-response/#jsonresponse-objects
# import it
from django.http import JsonResponse
def my_view(request):
# do something with the your data
data = {}
# just return a JsonResponse
return JsonResponse(data)
In the case of the JSON response there is no template to be rendered. Templates are for generating HTML responses. The JSON is the HTTP response.
However, you can have HTML that is rendered from a template withing your JSON response.
html = render_to_string("some.html", some_dictionary)
serialized_data = simplejson.dumps({"html": html})
return HttpResponse(serialized_data, mimetype="application/json")
For rendering my models in JSON in django 1.9 I had to do the following in my views.py:
from django.core import serializers
from django.http import HttpResponse
from .models import Mymodel
def index(request):
objs = Mymodel.objects.all()
jsondata = serializers.serialize('json', objs)
return HttpResponse(jsondata, content_type='application/json')
It looks like the Django REST framework uses the HTTP accept header in a Request in order to automatically determine which renderer to use:
http://www.django-rest-framework.org/api-guide/renderers/
Using the HTTP accept header may provide an alternative source for your "if something".
You could also check the request accept content type as specified in the rfc. That way you can render by default HTML and where your client accept application/jason you can return json in your response without a template being required
from django.utils import simplejson
from django.core import serializers
def pagina_json(request):
misdatos = misdatos.objects.all()
data = serializers.serialize('json', misdatos)
return HttpResponse(data, mimetype='application/json')
Here's an example I needed for conditionally rendering json or html depending on the Request's Accept header
# myapp/views.py
from django.core import serializers
from django.http import HttpResponse
from django.shortcuts import render
from .models import Event
def event_index(request):
event_list = Event.objects.all()
if request.META['HTTP_ACCEPT'] == 'application/json':
response = serializers.serialize('json', event_list)
return HttpResponse(response, content_type='application/json')
else:
context = {'event_list': event_list}
return render(request, 'polls/event_list.html', context)
you can test this with curl or httpie
$ http localhost:8000/event/
$ http localhost:8000/event/ Accept:application/json
note I opted not to use JsonReponse as that would reserialize the model unnecessarily.
If you want to pass the result as a rendered template you have to load and render a template, pass the result of rendering it to the json.This could look like that:
from django.template import loader, RequestContext
#render the template
t=loader.get_template('sample/sample.html')
context=RequestContext()
html=t.render(context)
#create the json
result={'html_result':html)
json = simplejson.dumps(result)
return HttpResponse(json)
That way you can pass a rendered template as json to your client. This can be useful if you want to completely replace ie. a containing lots of different elements.

Getting a JSON request in a view (using Django)

I am trying to set up a view to received a JSON notification from an API. I'm trying to figure out how to get the JSON data, and I currently have this as a starting point to see that the request is being properly received:
def api_response(request):
print request
return HttpResponse('')
I know the JSON object is there because in the print request it shows:
META:{'CONTENT_LENGTH': '178',
[Fri Sep 09 16:42:27 2011] [error] 'CONTENT_TYPE': 'application/json',
However, both of the POST and GET QueryDicts are empty. How would I set up a view to receive the JSON object so I can process it? Thank you.
This is how I did it:
def api_response(request):
try:
data=json.loads(request.raw_post_data)
label=data['label']
url=data['url']
print label, url
except:
print 'nope'
return HttpResponse('')
I'm going to post an answer to this since it is the first thing I found when I searched my question on google. I am using vanilla django version 3.2.9. I was struggling to retrieve data after making a post request with a json payload to a view. After searching for a while, I finally found the json in request.body.
Note: request.body is of type bytes, you'll have to decode it to utf-8, my_json_as_bytes.decode('utf-8')
or, if you want a dictionary, you can just use json.load(request.body) to decode directly.
On a function view, you can try this out.
dp = json.dumps(request.data)
contact = json.loads(dp)
print(contact['first_name'])
For Class-Based Views built Using Django-Rest-Framework,
you can use built-in JSONParser to get JSON data in request.data
from django.http import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework.views import APIView
class MyOveridingView(APIView):
parser_classes = [JSONParser]
class MyActualView(MyOveridingView):
def post(self, request, *args, **kwargs):
request_json = request.data
return JsonResponse(data=request_json, status=200)

Categories

Resources