Using Postman
When sending requests to an API, you can do so from within the JavaScript you write while developing your application. However, there is a multitude of factors that can prevent you from verifying whether you are formulating a request to an API in the correct manner (For example: incompatibile packages, misaligned package versions, configuration problems etc.). Therefore it can be very useful to use a stand-alone application, called Postman, to test your API requests and reponses.
Let's use Postman to verify the local JSON server we set up in the previous chapter!
- First, make sure you have downloaded the Postman desktop application for Mac (see Prerequisites chapter).
- Second, register your account on the Postman website and use it to log in to your Postman desktop application.
- Third, within the Postman desktop application, add
http://localhost:3004/time-entries
as the request URL. - Fourth, set the request method to GET (this is the default when building a new Postman request).
- Fifth, click the 'Headers' tab in Postman. The listed key-value pairs are default in a Postman request header. Add the following key-value pair:
Content-Type: application/json
. - Sixth, make sure your json-server is running.
- Finally, click the blue button 'Send' and verify the API response in the bottom panel.
You see? You haven't written any code to check whether your JSON server is running, which makes this a fool-proof way of checking whether it is the API, or yourself, that's making a mistake in fetching remote data.
Spoiler alert: it's usually due to your own mistake (in spelling, JavaScript, logic, syntax, life).