Now, we can launch Django's development server to compose and send HTTP requests.
Execute any of the following two commands based on your needs to access the API in other devices or computers connected to your LAN. Remember that we analyzed the difference between them in Chapter 1, Developing RESTful APIs with Django.
python manage.py runserver
python manage.py runserver 0.0.0.0:8000
After we run any of the previous commands, the development server will start listening at port 8000:
Now, we will compose and send an HTTP request to retrieve all the game categories whose name matches 3D RPG:
http :8000/game-categories/?name=3D+RPG
The following is the equivalent curl command:
curl -iX GET :8000/game-categories/?name=3D+RPG
The following lines show a sample response with the single game category whose name matches the specified name in the filter. The following lines only show the JSON body without the headers:
"url": "http://localhost:8000/game-categories/3/"
} ] }
We will compose and send an HTTP request to retrieve all the games whose related category id is equal to 3 and the value for the played field is equal to True. We want to sort the results by release_date in descending order, and therefore, we specify -release_date in the value for ordering. The hyphen (-) before the field name specifies the ordering feature to use descending order instead of the default ascending order. Make sure you replace 3 with the pk
value of the previously retrieved game category named 3D RPG. The played field is a bool
field, and therefore, we have to use Python-valid bool values (True and False) when specifying the desired values for the bool field in the filter:
http ':8000/games/?game_category=3&played=True&ordering=-release_date'
The following is the equivalent curl command:
curl -iX GET ':8000/games/?game_category=3&played=True&ordering=-release_date'
The following lines show a sample response with the two games that match the specified criteria in the filter. The following lines only show the JSON body without the headers:
{
In the GameList class, we specified 'game_category' as one of the strings in the
filter_fields tuple of string. Thus, we had to use the game category id in the filter. Now, we will use a filter on the game's name related to a registered score. The PlayerScoreFilter
class provides us a filter to the name of the related game in game_name. We will combine the filter with another filter on the player's name related to a registered score. The
PlayerScoreFilter class provides us a filter to the name of the related player in player_name. Both conditions specified in the criteria must be met, and therefore, the filters are combined with the AND operator:
http ':8000/player-scores/?player_name=Kevin&game_name=Superman+vs+Aquaman'
The following is the equivalent curl command:
curl -iX GET ':8000/player-scores/?
player_name=Kevin&game_name=Superman+vs+Aquaman'
The following lines show a sample response with the score that matches the specified criteria in the filters. The following lines only show the JSON body without the headers:
{ "count": 1,
We will compose and send an HTTP request to retrieve all the scores that match the following criteria. The results will be ordered by score_date in descending order.
The score value is between 30,000 and 150,000
The score_date is between 2016-06-21 and 2016-06-22
http
':8000/player-scores/?score=&from_score_date=2016-06- 01&to_score_date=2016-06-28&min_score=30000&max_score=150000&ordering=-score_date'
The following is the equivalent curl command:
curl -iX GET
':8000/player-scores/?score=&from_score_date=2016-06- 01&to_score_date=2016-06-28&min_score=30000&max_score=150000&ordering=-score_date'
The following lines show a sample response with the three games that match the specified criteria in the filters. We overrode the default ordering specified in the model with the
specified ordering in the request. The following lines only show the JSON body without the headers:
},
In the preceding requests, all the responses didn't have more than one page. In case the
response requires more than one page, the values for the previous and next keys will display the URLs that include the combination of the filters, search, ordering and pagination.
We will compose and send an HTTP request to retrieve all the games whose name starts with
'S'. We will use the search feature that we configured to restrict the search behavior to a starts-with match on the name field:
http ':8000/games/?search=S'
The following is the equivalent curl command:
curl -iX GET ':8000/games/?search=S'
The following lines show a sample response with the two games that match the specified search criteria, that is, those games whose name starts with 'S'. The following lines only show the JSON body without the headers:
{ "count": 2,
{
"game_category": "3D RPG", "name": "Superman vs Aquaman", "owner": "superuser",
"played": true,
"release_date": "2016-06-21T03:02:00.776594Z", "url": "http://localhost:8000/games/3/"
} ] }
Tip
We can change the search and ordering parameter's default names: 'search' and 'ordering'. We just need to specify the desired names in the SEARCH_PARAM and the ORDERING_PARAM
settings.