1

I have an Angular/DRF application.

What I want is to send an object to my backend to process it:

 var data = {'url': url, 'name': name}
 return this.http.get("http://127.0.0.1:8000/api/record/json=" + encodeURIComponent(JSON.stringify(data)))

Because my data as URL in it, I URI encode it to escape the "/"

Then in DRF this my urls.py:

path('record/<str:music_details>', record_views.Record.as_view(), name='record'),

And my views.py:

class Record(View):

    def get(self, request):
        json_string = request.query_params['json']
        data = json.loads(json_string)
        print(data)

I have nothing printing in my console, I assume my url path is wrong, how should I fix it?

2
  • Your request looks to have an extra /api preceding the record part of the url. Unless you're explicitly adding this somewhere in django, I don't think you need it Commented May 7, 2021 at 9:36
  • I need it because every incoming requests strarting with /api are redirected to this urls.py Commented May 7, 2021 at 9:40

1 Answer 1

3

Solution 1:

Send query parameter after ? like:

var data = {'url': url, 'name': name}
return this.http.get("http://127.0.0.1:8000/api/record?json=" + encodeURIComponent(JSON.stringify(data)))

Modify urls.py:

path('record', record_views.Record.as_view(), name='record'),

and fetch from query as:

json_string = request.GET.get('json')

Solution 2:

Send data with json= like:

var data = {'url': url, 'name': name}
return this.http.get("http://127.0.0.1:8000/api/record/" + encodeURIComponent(JSON.stringify(data)))

and modify get method to fetch data as:

class Record(View):

    def get(self, request, music_details):
        data = json.loads(music_details)
        print(data)
Sign up to request clarification or add additional context in comments.

2 Comments

First is working fine. What's the diference between the two?
In solution 1, data is being sent in query string or get method, which is in key:value form and starts after ?. In solution 2, data included as URI itself which are called query parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.