Im building a simple api in Django Rest Framework using class based views and the documentation mentions request.data (https://www.django-rest-framework.org/tutorial/2-requests-and-responses/)
from rest_framework.views import APIView
class myView(APIView):
def post(self, request):
print(request.data)
When I try to send post requests either with
curl: curl --data param1=val1¶m2=val2 url
djangos browser interface with post (using formatted url and the post option
Advanced Rest Client (chrome add on)
The data of all three appears to end up in request.query_params.
When I try to print request.data, however I get an empty response {}
I could just use request.query_params, Im just curious as to how one would get data to go to request.data since it is talked about in the documentation
edit: so curl --data param1=val1¶m2=val2 -X POST url sends information to request.data (now if I say request.query_params if get the empty dict {} but request.data contains an object that is the dict query_params.)
curl request doesn't have the -X and POST added.
Add these
Related
I have django app in it's views.py created for accepting, and saving objects from received data into database.
from .models import Hook
def hooktrack(request):
source=requests.POST.get("source")
b=Hook(dsourse=source).save()
Than I link this view to .urls.py
urlpatterns = [
path("hooktrack/",hooktrack),
]
My full url use http protockol not https
maybe it involved in my problem
I several times tested hooktrack view and it was accept and save received data.
curl -d "source=google" -X POST http://example.net/hooktrack/
After curling sent source=value accessible to query from dB.
from my.models import Hook
a=Hook.objects.all()
for item in a:
print(item.dsource)
...google
Than I linked my hooktrack view to production for accepting hooks from our business partner. After receiving them all of them saved to database but has None value
Updated
I inspected my Nginx logs and found that POST request and it is looking isn't as json
[05/Oct/2020:17:46:59 +0000] "POST /calltrackinghook/?dsource=google&dmedium=cpc&dcampaign=10393090041&dterm=+%D1%85%D0%BE%D0%BB%D0%BE%D0%B4%D0%B8%D0%BB%D1%8C%D0%BD%D0%B8%D0%BA%20+%D0%B1%D0%BE%D1%88_b&dcontent=gclid=CjwKCAjwiOv7BRBREiwAXHbv3O6RZTQ_CCIENfrX0FJqtKBFhPmhF6gZFjbewnG-P-UUnHQn_5n7ZhoCtmwQAvD_BwE&disposition=ANSWERED&virtual_number=8005337639&caller=9324034020&real_number=17612021180&uniqueid=1601920016.1303437&datetime=2020-10-05%2020:46:56&duration=0&recordlink=https%3A%2F%2Fcalltracking.ru%2FshareRecords%2F2020-10-05%2F1601920016.1303437.mp3&project_id=9892&source_id=&accountcode=&landing=https%3A%2F%2Fm.05.ru%2F&client_landing=&source_name=Google.Adwords&call_region=%D0%A5%D0%B0%D0%BD%D1%82%D1%8B-%D0%9C%D0%B0%D0%BD%D1%81%D0%B8%D0%B9%D1%81%D0%BA%D0%B8%D0%B9%20%D0%90%D0%9E&referrer=http%3A%2F%2Fwww.google.com%2F&cid
POST parameters available when you submit data in the application/x-www-form-urlencoded format (e.g., using a HTML form). So request 'Content-Type' must be application/x-www-form-urlencoded or multipart/form-data.
If data send as JSON, you can access your JSON data as follows:
import json
def hooktrack(request):
data = json.loads(request.body)
# Data is now a python dict, e.g.,
Maybe also interesting for you, csrf_exempt decorator.
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def hooktrack(request):
source=requests.POST.get("source")
b=Hook(dsourse=source).save()
...
I build SPA on Django and I want to GET and POST JSON data based on request.user.
Something like this:
def get(self, *args, **kwargs):
return {
"data": [
i.get_json() for i in Customer.objects.filter(pk=self.request.user.pk)]
}
But I confuse, how it possible to put my user in request by REST service, like "Postman" or "Curl".
Postman has "Authorization" field, so I put login and password into it and my headers update with:
Authorization Basic YWdlbmN5X3NwYUBtYWlsLnJ1OjExMTEx
And I test it with curl:
curl -u myuser:11111 http://127.0.0.1:8000/api/myurl/
But user still - AnonymousUser
It could work with angular later, but I don't understand how I can test it now.
I found solution. I need to login with my user first and take sessionid from Cookie, then put sessionid in request.
Postman has nice extension named "Postman Interceptor", it put all session from browser into request authomaticly.
Hi i am making an webserver , In which I have to hit some request from html page and return the response. The URL which is generated using html is
http://192.168.2.253:8080/searchSMS/?KPImsgId=0&circle=&subId=&startDate=DD-MM-YYYY&endDate=DD-MM-YYYY&Username=ashish
but in the server side I am not able to see the request data. I am using
q = QueryDict(request.body) but it is showing <QueryDict: {}>
How to find the all the parameters coming in request.
In your case you send the data in url so access the data through request.GET as follow:
username = request.GET.get('Username')
start_date = request.GET.get('startDate')
# ... the same for all the other parameter after the `?` marque.
In fact there is a difference between request data, request.body, request.GET and request.POST:
If you are sending POST request to django function view or class based view: you access the request data in request.body or request.POST.
If you are sending POST request to Django REST Framework: you access the data in request.data. You may also find in Internet request.DATA that correct but it's deprecated in the newer version of DRF in favor of request.data.
If you send parameter in the url like in you case, you access the data form request.GET as explained above.
Im using DJango REST framework to write my web service layer in which I want to read request payload from the request (POST). I tried the following code but I get empty set
#api_view(['POST'])
def login(request):
print request.POST
Content Type is JSON. I tried to pass data from REST Client Tool. Still am able to read header values but only Payload is not coming out.
You should use request.DATA instead of request.POST to retrieve json data.
request.DATA has been deprecated in favor of request.data since version 3.0, and has been fully removed as of version 3.2.
Django request handler let you get POST parameters using request.DATA
def post(self, request):
request.DATA.
How could I get DELETE parameters?
I tried
def delete(self,request):
request.body
request.read()
Both request.body and request.read() just displays csrfmiddlewaretoken and _method parameters.
For both of the above example I am sending the parameters as application/json.
How could I get the delete request parameters?
I know this question is quite old, but for reference: I think you might want Request.QUERY_PARAMS. See the REST Framework docs for more info: http://django-rest-framework.org/api-guide/requests.html