Testing Endpoints

Before the bird-spotting event, you better test your web service. You spin it up and visit http://localhost:5000/species in your browser. This is the JSON that you see:

{"ok":true,"results":{}}

Try visiting this URL in Firefox, which offers a more structured display of JSON text.

This is the JSON you expect. No species have been counted yet. You go on to test the endpoint that yields the count of a named species by visiting http://localhost:5000/species/indigo%20bunting. This is the result:

{"ok":true,"results":{"species":"indigo bunting","count":0}}

Your service is holding up great. Next you want to test spotting an indigo bunting. You visit the endpoint for incrementing a count, which has the URL http://localhost:5000/species/indigo%20bunting. But wait. That's the same URL as the one for getting a count. To increment a count, you need to issue a POST request to the endpoint. The browser only issues GET requests for the URLs you enter in the location bar.

You won't be able to test much of your service with only the browser. A more powerful tool is cURL, which is available on all major operating systems and may even already be installed on your computer. Try running curl in a terminal. If it's not found, find a way to install it.

Once you have cURL installed, test the first two endpoints with this command:

curl http://localhost:5000/species
curl http://localhost:5000/species/indigo%20bunting

In case you're curious about what the HTTP request and response look like, turn on verbose output using the -v flag:

curl -v http://localhost:5000/species/indigo%20bunting

To increment a species, switch to the POST method with the --request command-line:

curl --request POST \
http://localhost:5000/species/indigo%20bunting

The response shows the incremented counter. Try hitting the get-all endpoint again. Does it also yield the correct count? Try spotting a new species several times. Do you see the correct counts for both species when you hit the get-all endpoint?

To delete a species, switch to the DELETE method:

curl --request DELETE \
http://localhost:5000/species/indigo%20bunting

Does the get-all endpoint show that the indigo bunting count has been removed? Try removing a species that doesn't exist. Include -v flag to check the status code.

Testing the patch-all endpoint is a bit more work. You must add a header that describes the encoding of the body, and you must also add the body itself using the --data command-line argument:

curl -v \
--request PATCH \
--header 'Content-Type: application/json' \
--data '{"cardinal": 5, "goldfinch": 10}' \
http://localhost:5000/species

Try running this command. Does the get-all endpoint show that cardinal and goldfinch have been added to the counts?