Making HTTP GET requests that target a collection of instances

We installed command-line and GUI tools that were going to allow us to compose and send HTTP requests to the web services we were going to build throughout this book. Now, we will use the curl utility to make HTTP GET requests, specifically, HTTP GET requests that target a collection of toys. In case curl is not included in the path, make sure you replace curl with the full path to this utility.

Make sure you leave the Django development server running. Don't close the terminal or Command Prompt that is running this development server. Open a new Terminal in Linux or macOS, or a Command Prompt in Windows, and run the following command. It is very important that you enter the ending slash (/) because /toys won't match any of the patterns specified in urlpatterns in the toys/urls.py file. We aren't going to use options to follow redirects. Thus, we must enter /toys/, including the ending slash (/).

    curl -X GET localhost:8000/toys/

The previous command will compose and send the following HTTP request: GET http://localhost:8000/toys/. The request is the simplest case in our RESTful Web Service because it will match and run the views.toy_list function, that is, the toy_list function we coded within the toys/views.py file. The function just receives request as a parameter because the URL pattern doesn't include any parameters. As the HTTP verb for the request is GET, the request.method property is equal to 'GET', and therefore, the function will execute the code that retrieves all the Toy objects and generates a JSON response with all of these Toy objects serialized.

The following lines show an example response for the HTTP request, with three Toy objects in the JSON response:

    [{"pk":3,"name":"Clash Royale play set","description":"6 figures from Clash Royale","release_date":"2017-10-09T12:10:00.776594Z","toy_category":"Playset","was_included_in_home":false},{"pk":2,"name":"Hawaiian Barbie","description":"Barbie loves Hawaii","release_date":"2017-10-09T12:11:37.090335Z","toy_category":"Dolls","was_included_in_home":true},{"pk":1,"name":"Snoopy talking action figure","description":"Snoopy speaks five languages","release_date":"2017-10-09T12:11:37.090335Z","toy_category":"Action figures","was_included_in_home":false}]

As we might notice from the previous response, the curl utility displays the JSON response in a single line, and therefore, it is a bit difficult to read. It is possible to use different tools, including some Python scripts, to provide a better format to the response. However, we will use the HTTPie command-line tool we installed in our virtual environment for this purpose later.

In this case, we know that the value of the Content-Type header key of the response is application/json. However, in case we want more details about the response, we can use the -i option to request curl to print the HTTP response headers and their key-value pairs. We can combine the -i and -X options by entering -iX.

Go back to the terminal in Linux or macOS, or the Command prompt in Windows, and run the following command:

    curl -iX GET localhost:8000/toys/

The following lines show an example response for the HTTP request. The first lines show the HTTP response headers, including the status (200 OK) and the Content-Type: application/json. After the HTTP response headers, we can see the details for the three Toy objects in the JSON response:

    HTTP/1.0 200 OK
    Date: Tue, 10 Oct 2017 00:53:41 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    Content-Type: application/json
    X-Frame-Options: SAMEORIGIN
    Content-Length: 548
    
[{"pk":3,"name":"Clash Royale play set","description":"6 figures from Clash Royale","release_date":"2017-10-09T12:10:00.776594Z","toy_category":"Playset","was_included_in_home":false},{"pk":2,"name":"Hawaiian Barbie","description":"Barbie loves Hawaii","release_date":"2017-10-09T12:11:37.090335Z","toy_category":"Dolls","was_included_in_home":true},{"pk":1,"name":"Snoopy talking action figure","description":"Snoopy speaks five languages","release_date":"2017-10-09T12:11:37.090335Z","toy_category":"Action figures","was_included_in_home":false}]

After we run the two requests, we will see the following lines in the window running the Django development server. The output indicates that the server received two HTTP requests with the GET verb and /toys/ as the URI. The server processed both HTTP requests, returned a status code equal to 200, and the response length was equal to 548 characters.

The response length might be different because the value for the primary key assigned to each toy will have an incidence in the response length. The first number after HTTP/1.1." indicates the returned status code (200) and the second number the response length (548):

    [09/Oct/2017 22:12:37] "GET /toys/ HTTP/1.1" 200 548
    [09/Oct/2017 22:12:40] "GET /toys/ HTTP/1.1" 200 548

The following image shows two Terminal windows side-by-side on macOS. The Terminal window on the left-hand side is running the Django development server and displays the received and processed HTTP requests. The Terminal window on the right-hand side is running curl commands to generate the HTTP requests. It is a good idea to use a similar configuration to check the output while we compose and send the HTTP requests. Notice that the JSON outputs are a bit difficult to read because they don't use syntax highlighting:

Now, open a new Terminal in Linux or macOS, or a new Command Prompt in Windows, and activate the virtual environment we created. This way, you will be able to access the HTTPie utility we installed within the virtual environment.

We will use the http command to easily compose and send HTTP requests to localhost:8000 and test the RESTful Web Service. HTTPie supports curl-like shorthand for localhost, and therefore we can use :8000 as a shorthand that expands to http://localhost:8000. Run the following command and remember to enter the ending slash (/):

 http :8000/toys/

The previous command will compose and send the following HTTP request: GET http://localhost:8000/toys/. The request is the same one we previously composed with the curl command. However, in this case, the HTTPie utility will display a colorized output and it will use multiple lines to display the JSON response, without any additional tweaks. The previous command is equivalent to the following command that specifies the GET method after http:

http :8000/toys/

The following lines show an example response for the HTTP request, with the headers and the three Toy objects in the JSON response. It is indeed easier to understand the response compared with the results that were generated when we composed the HTTP request with curl. HTTPie automatically formats the JSON data received as a response and applies syntax highlighting, specifically, both colors and formatting:

    HTTP/1.0 200 OK
    Content-Length: 548
    Content-Type: application/json
    Date: Tue, 10 Oct 2017 01:26:52 GMT
    Server: WSGIServer/0.2 CPython/3.6.2
    X-Frame-Options: SAMEORIGIN
    
    [
        {
            "description": "6 figures from Clash Royale",
            "name": "Clash Royale play set",
            "pk": 3,
            "release_date": "2017-10-09T12:10:00.776594Z",
            "toy_category": "Playset",
            "was_included_in_home": false
        },
        {
            "description": "Barbie loves Hawaii",
            "name": "Hawaiian Barbie",
            "pk": 2,
            "release_date": "2017-10-09T12:11:37.090335Z",
            "toy_category": "Dolls",
            "was_included_in_home": true
        },
        {
            "description": "Snoopy speaks five languages",
            "name": "Snoopy talking action figure",
            "pk": 1,
            "release_date": "2017-10-09T12:11:37.090335Z",
            "toy_category": "Action figures",
            "was_included_in_home": false
        }
    ] 
We can achieve the same results by combining the output generated with the curl command with other utilities. However, HTTPie provides us exactly what we need for working with RESTful Web Services such as the one we are building with Django. We will use HTTPie to compose and send HTTP requests, but we will always provide the equivalent curl command. Remember that curl is faster when you need to execute it many times, such as when you prepare automated scripts.

The following image shows two Terminal windows side-by-side on macOS. The Terminal window on the left-hand side is running the Django development server and displays the received and processed HTTP requests. The Terminal window on the right-hand side is running HTTPie commands to generate the HTTP requests. Notice that the JSON output is easier to read compared to the output generated by the curl command:

We can execute the http command with the -b option in case we don't want to include the header in the response. For example, the following line performs the same HTTP request but doesn't display the header in the response output, and therefore, the output will just display the JSON response:

    http -b :8000/toys/
..................Content has been hidden....................

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