I am new to Python and Django frameworks. I have Rest API's developed using Django framework, how to access Rest API's and display response in Python Console application?
Thanks
CodePudding user response:
If you want to go all the way you need a http client in python, A popular one is requests
to use this in terminal you can just do
import requests
response = requests.get('http://localhost/api/myview')
print(response.status_code)
print(response.text)
print(response.json)
The drawback to this is that the server actually has to be running, if you want to play around without starting a server you have more options!
You can also use the test clients built into Django, like this
from rest_framework.test import APIClient
client = APIClient()
response = client.get('/api/myview/')
Or even construct a Request and call the view directly.
from rest_framework.test import APIRequestFactory
from myproject.views import MyViewClass
view = MyViewClass.as_view()
factory = APIRequestFactory()
request = factory.get('/api/myview/')
response = view(request)