5

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.

Savad KP
  • 1,625
  • 3
  • 28
  • 40
Ashish Kumar Verma
  • 1,322
  • 1
  • 13
  • 21

1 Answers1

7

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.
Dhia
  • 10,119
  • 11
  • 58
  • 69
  • i got your point but how to get in this case `http://192.168.2.253:8080/searchCall/?startDate=DD-MM-YYYY&endDate=DD-MM-YYYY&reportingType=0&circle=0&kpi=107&kpi=121` I can get more than one kpi – Ashish Kumar Verma Dec 23 '15 at 08:01
  • 2
    The parameter are send as keys of a dictionary, so if there is a duplicate only one will be considered. So either you send them with different name or in list. Check this post for more details about [using a list or not](http://stackoverflow.com/a/9176496/5658350) – Dhia Dec 23 '15 at 09:19
  • can't I do `QueryDict(request.body)` in this after changing names. – Ashish Kumar Verma Dec 23 '15 at 09:46
  • `request.body` contains only the data send in the body of a POST request but it can not contain the url parameters. – Dhia Dec 23 '15 at 09:48
  • so that means I dont have any way to do it :( – Ashish Kumar Verma Dec 23 '15 at 09:50
  • I just want to check the parameters coming from url request. – Ashish Kumar Verma Dec 23 '15 at 09:53
  • 2
    @DhiaTN it is not true that only one value per key will be considered. It is perfectly valid to send multiple values; in which case you can use `request.GET.getlist('param_name')` to get them all as a list. – Daniel Roseman Dec 23 '15 at 09:55
  • Hi @DanielRoseman I already mention that in the comment above in the link: `?id[]=101404&id[]=7267261` – Dhia Dec 23 '15 at 09:59
  • But that is not true. In Django there is no need to use that PHP/Rails format; `id=123&id=456` works fine. – Daniel Roseman Dec 23 '15 at 10:05
  • Ah okay thanks, I got it. I did not know about `GET.getlist`,@AshishK. check [here](http://stackoverflow.com/a/12955933/5658350) this is what Daniel is talking about – Dhia Dec 23 '15 at 10:06