Getting a JSON request in a view (using Django) - python

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)

Related

How do I use a 'q' to call an API and get a response in Django views?

I'm trying to query this api and get a response but I'm obviously not doing it correctly as i know its not as simple as this.
from django.shortcuts import render
def home(request):
response = request.GET["https://api-adresse.data.gouv.fr/search/?q=8+bd+du+port"}
return render(request, "home.html", {'response': response})
I would like to be able to give an address to the API ie: "8 Boulevard du Port 80000 Amiens" and
get the associated information about it. ie: gps coordinates
this is the api: https://adresse.data.gouv.fr/api
I can't seem to find information on how to make this kind of request and handle the response using views in django.
request.GET is a dictionary-like object containing all given HTTP GET parameters also called Query parameters or Query string.
requests is a simple HTTP library for Python that allows you to send HTTP/1.1 requests.
from urllib.parse import quote_plus
import requests
from django.shortcuts import render
def home(request):
url = "https://api-adresse.data.gouv.fr/search/?q={}"
address = "8 Boulevard du Port 80000 Amiens"
response = requests.get(url.format(quote_plus(address)))
# response.json() # this will give you JSON response
return render(request, "home.html", {"response": response})

Django Rest Framework way to ingest json data?

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

Django receiving json post request from external source

I have written a view function that processes a post request containing json data from a source outside of django (labview). I'm just testing it to begin with so it looks like this
def post_entry(request):
'''Process incoming json string
'''
if request.method == 'POST':
post_data = request.body
# Return a response
return HttpResponse('data received OK')
I've written a test to test this and it passes fine:
def test_post_entry_view_good_post_data(self):
'''post_entry view should return a 200 status if valid
'''
data = {'DHTP Data': ['10', '50.296', '50.94', '50.418', '50.425', '50.431', '50.94'],
'Test String': 'My Test String'}
request_url = reverse('post_entry')
response = self.client.post(request_url, content_type='application/json',
data=dumps(data))
# Should return a 200 response indicating ok
self.assertEqual(response.status_code, 200)
But when labview posts the data post_entry returns a 403 forbidden error. I guess this is due to no csrf token being present, but why does the test pass in this case?
The test client works around the CSRF functionality. See https://docs.djangoproject.com/en/1.9/ref/csrf/#testing
If you are going to have a view that accepts post data from a source external to your app you need to make your view exempt from CSRF protection by using csrf_exempt:
#csrf_exempt
def post_entry(request):
'''Process incoming json string
'''
If you are going to do this, you should use some other method of validating the request
If your view is supposed to accept POST from external sources it is upon you to validate the request as every POST request is required to have a CSRF token (Refer: CSRF). Hence, for your purpose, you'll have to exempt the view from CSRF validation using #csrf_exempt decorator and write your own validation for the request using something like Token Authentication
Use this line to get the decorator needed to bypass CSRF protection:
from django.views.decorators.csrf import csrf_exempt
then put the #csrf_exempt decorator on the line before your function.

Sending POST request with JSON data in DJANGO and response from view also JSON data but its giving 403 FORBIDDEN error

I am trying to send a POST request to Django with JSON data in it and the view is returning response with JSON data. But when I send a request to it, it returns with 403 Forbidden error. I am using RESTClient to send/test POST requests.
I have read all about CSRF in documentation but its not very helpful. I am fairly new to Django and the other questions posted here are not helping me a lot.
The code in my view is:
from django.shortcuts import render
from django.http import HttpResponse;
import json;
def index(request):
if request.is_ajax():
if request.method == 'POST':
print 'Raw Data: "%s"' % request.body;
reply = json.loads(request.body);
return HttpResponse(reply);
else:
return HttpResponse("OK");
else:
return HttpResponse("OK");
In addition to #ArpitGoyal's answer you can also decorate your view with csrf_exempt:
This decorator marks a view as being exempt from the protection ensured by the middleware.
A few tips in case you do need CSRF protection:
Check CSRF token cookie name.
See CSRF_COOKIE_NAME for more information.
Add ensure_csrf_cookie decorator to your view.
According to the docs:
Warning
If your view is not rendering a template containing the csrf_token template tag, Django might not set the CSRF token cookie. This is common in cases where forms are dynamically added to the page. To address this case, Django provides a view decorator which forces setting of the cookie: ensure_csrf_cookie().
Assuming that CSRF token cookie name is csrftoken, try to send X-CSRFToken header.
$.ajax({
// Your options here.
headers: {'X-CSRFToken': getCookie('csrftoken')}
});
You should authenticate your client before making the request. From your call you are providing a ajax POST request hit.
Provide a header in your RESTClient: X-CSRFToken.
For more details view this

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.

Categories

Resources