Reputation: 183
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?
Upvotes: 1
Views: 1890
Reputation: 4840
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)
Upvotes: 3