Making requests that paginate results

Now, we will compose and send an HTTP GET request to retrieve all the drones. The new pagination settings will take effect and we will only retrieve the first page for the drones resource collection:

    http GET :8000/drones/  

The following is the equivalent curl command:

    curl -iX GET localhost:8000/drones/

The previous commands will compose and send an HTTP GET request. The request specifies /drones/, and therefore, it will match the '^drones/$' regular expression and run the get method for the views.DroneList class-based view. The method executed in the generic view will use the new settings we added to enable the offset/limit pagination, and the result will provide us with the first four drone resources. However, the response body looks different than in the previous HTTP GET requests we made to any resource collection. The following lines show the sample response that we will analyze in detail. Don't forget that the drones are being sorted by the name field, in ascending order:

    HTTP/1.0 200 OK
    Allow: GET, POST, HEAD, OPTIONS
    Content-Length: 958
    Content-Type: application/json
    Date: Mon, 06 Nov 2017 23:08:36 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "count": 11, 
        "next": "http://localhost:8000/drones/?limit=4&offset=4", 
        "previous": null, 
        "results": [
            {
                "drone_category": "Quadcopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-03T01:59:31.108031Z", 
                "manufacturing_date": "2017-08-18T02:02:00.716312Z", 
                "name": "Atom", 
                "url": "http://localhost:8000/drones/2"
            }, 
            {
                "drone_category": "Octocopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-06T20:25:30.357127Z", 
                "manufacturing_date": "2017-04-18T02:02:00.716312Z", 
                "name": "Dassault Falcon 7X", 
                "url": "http://localhost:8000/drones/6"
            }, 
            {
                "drone_category": "Quadcopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-06T20:25:31.049833Z", 
                "manufacturing_date": "2017-07-20T02:02:00.716312Z", 
                "name": "Dusty", 
                "url": "http://localhost:8000/drones/9"
            }, 
            {
                "drone_category": "Octocopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-06T20:25:29.909965Z", 
                "manufacturing_date": "2017-02-18T02:02:00.716312Z", 
                "name": "Eclipse", 
                "url": "http://localhost:8000/drones/4"
            }
        ]
    }

The response has a 200 OK status code in the header and the following keys in the response body:

  • count: The value indicates the total number of drones for the query.
  • next: The value provides a link to the next page.
  • previous: The value provides a link to the previous page. In this case, the response includes the first page of the result set, and therefore, the link to the previous page is null.
  • results: The value provides an array of JSON representations of Drone instances that compose the requested page. In this case, the four drones belong to the first page of the result set.

In the previous HTTP GET request, we didn't specify any values for either the limit or offset parameters. We specified 4 as the default value for the limit parameter in the global settings and the generic views use this configuration value and provide us with the first page. Whenever we don't specify any offset value, the default offset is equal to 0 and the get method will return the first page.

The previous request is equivalent to the following HTTP GET request that specifies 0 for the offset value. The result of the next command will be the same as the previous one:

    http GET ":8000/drones/?offset=0"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?offset=0"

The previous requests are equivalent to the following HTTP GET request that specifies 0 for the offset value and 4 for the limit value. The result of the next command will be the same as the previous two commands:

    http GET ":8000/drones/?limit=4&offset=0"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?limit=4&offset=0"
  

Now, we will compose and send an HTTP request to retrieve the next page, that is, the second page for the drones. We will use the value for the next key provided in the JSON body of the response from the previous requests. This value gives us the URL for the next page: http://localhost:8000/drones/?limit=4&offset=4. Thus, we will compose and send an HTTP GET method to /drones/ with the limit value set to 4 and the offset value set to 4 :

    http GET ":8000/drones/?limit=4&offset=4"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?limit=4&offset=4"

The result will provide us the second page of four drone resources as the value for the results key in the response body. In addition, we will see the values for the count, previous, and next keys that we analyzed in the previous requests. The following lines show the sample response:

HTTP/1.0 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 1007
Content-Type: application/json
Date: Mon, 06 Nov 2017 23:31:34 GMT
Server: WSGIServer/0.2 CPython/3.6.2
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
"count": 11,
"next": "http://localhost:8000/drones/?limit=4&offset=8",
"previous": "http://localhost:8000/drones/?limit=4",
"results": [
{
"drone_category": "Quadcopter",
"has_it_competed": false,
"inserted_timestamp": "2017-11-06T20:25:30.127661Z",
"manufacturing_date": "2017-03-20T02:02:00.716312Z",
"name": "Gossamer Albatross",
"url": "http://localhost:8000/drones/5"
},
{
"drone_category": "Quadcopter",
"has_it_competed": false,
"inserted_timestamp": "2017-11-06T20:25:30.584031Z",
"manufacturing_date": "2017-05-20T02:02:00.716312Z",
"name": "Gulfstream I",
"url": "http://localhost:8000/drones/7"
},
{
"drone_category": "Quadcopter",
"has_it_competed": false,
"inserted_timestamp": "2017-11-06T20:25:29.636153Z",
"manufacturing_date": "2017-01-20T02:02:00.716312Z",
"name": "Need for Speed",
"url": "http://localhost:8000/drones/3"
},
{
"drone_category": "Octocopter",
"has_it_competed": false,
"inserted_timestamp": "2017-11-06T20:25:30.819695Z",
"manufacturing_date": "2017-06-18T02:02:00.716312Z",
"name": "RV-3",
"url": "http://localhost:8000/drones/8"
}
]
}

In this case, the result set is the second page, and therefore, we have a value for the previous key: http://localhost:8000/drones/?limit=4.

In the previous HTTP request, we specified values for both the limit and offset parameters. However, as we set the default value of limit to 4 in the global settings, the following request will produce the same results as the previous request:

    http GET ":8000/drones/?offset=4"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?offset=4"

Now, we will compose and send an HTTP request to retrieve the next page, that is, the third and last page for the drones. We will use the value for the next key provided in the JSON body of the response from the previous requests. This value gives us the URL for the next page as http://localhost:8000/drones/?limit=4&offset=8. Thus, we will compose and send an HTTP GET method to /drones/ with the limit value set to 4 and the offset value set to 8 :

    http GET ":8000/drones/?limit=4&offset=8"

The following is the equivalent curl command:

    curl -iX GET "localhost:8000/drones/?limit=4&offset=8"

The result will provide us with the third and last page of three drone resources as the value for the results key in the response body. In addition, we will see the values for the count, previous, and next keys that we analyzed in the previous requests. The following lines show the sample response:

    HTTP/1.0 200 OK
    Allow: GET, POST, HEAD, OPTIONS
    Content-Length: 747
    Content-Type: application/json
    Date: Tue, 07 Nov 2017 02:59:42 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    Vary: Accept, Cookie
    X-Frame-Options: SAMEORIGIN
    
    {
        "count": 11, 
        "next": null, 
        "previous": "http://localhost:8000/drones/?limit=4&offset=4", 
        "results": [
            {
                "drone_category": "Octocopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-06T20:25:31.279172Z", 
                "manufacturing_date": "2017-08-18T02:02:00.716312Z", 
                "name": "Ripslinger", 
                "url": "http://localhost:8000/drones/10"
            }, 
            {
                "drone_category": "Quadcopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-06T20:25:31.511881Z", 
                "manufacturing_date": "2017-09-20T02:02:00.716312Z", 
                "name": "Skipper", 
                "url": "http://localhost:8000/drones/11"
            }, 
            {
                "drone_category": "Quadcopter", 
                "has_it_competed": false, 
                "inserted_timestamp": "2017-11-03T01:58:49.135737Z", 
                "manufacturing_date": "2017-07-20T02:02:00.716312Z", 
                "name": "WonderDrone", 
                "url": "http://localhost:8000/drones/1"
            }
        ]
    }  

In this case, the result set is the last page, and therefore, we have null as the value for the next key.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.47.253