279 total views, 1 views today
Working as a full stack developer for Cup O Code allows me the opportunity to work on both front-end and back-end projects. Throughout my time as a developer, I have come across many tool that help streamline my workflow and overall make life easier. One of my favorite tools in my developer tool bag is an API testing tool known as Postman. Postman is offered at both a free tier and paid plans that are very affordable for an entire team of developers. Information on pricing and official documentation can be found at https://www.postman.com/
To first understand the use of this tool, we should take a basic look at API’s or Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API. When working on both front-end and back-end projects, it is useful to test out the API on both end. When building the API on the back-end, the developer will need to test end-points to ensure they are working and delivering the data needed by the front-end. An API Endpoint is the URL for a server or a service. These APIs operate through responses and requests — that is you make a request and the API Endpoint makes a response.
One particular API that I am fond of and are using for an upcoming tutorial build is NASA’s photo of the day API. The API call’s we make will be sent to https://api.nasa.gov/planetary/apod. A quick copy and paste of this in to your browsers search bar will return an error as a result of not having an API key. Many developers of open API’s will require the user to create a unique API key that will ensure that calls made to the API do not exceed a certain limit. NASA however allows the developer to use a DEMO KEY. So now if we relook at that link and add an API key to our query, our URL will look like https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY. Notice the “?” in the URL is where we begin the query. Later in the demo I will show how other parameters are added to the query to get our desire result.
When interacting with an API and while building many applications, we have several types of requests. GET, POST, PUT and DELTE are the 4 common and most used calls to an API and are fairly self explanatory. A GET request pulls in specific data based a query and is to be read by the user. A POST request is the creation of data, i.e. adding a new user in an application. a PUT request updates an already created piece of data such as updating a users phone number. And a DELETE request, well, I will let you come to your own conclusion.
So great, we have the basics covered and to be fair this was supposed to be a much shorter blog. Let’s go ahead now with we have learned and show how we use Postman to check our API and get a response. For this we are going to be making a basic GET request to the NASA Photo of the Day API to get the information we would use to build out a simple website.
Let’s open up Postman
Here you will see the new workspace that we will be using to make our simple API call.
We are going to be making a GET request, so we can leave that option alone and begin to enter our URL. Let’s go ahead and copy the URL form earlier, this alone will get us information for the current date. But, let’s change things up a bit a pick our own date to view the data for. For this I will use a date that is special to me, August 11th 2008. In order to add this date to our query, we will need to add a date parameter that will look like date=2008-08-11, notice the date is in the YYYY-MM-DD format for our query. We will add this to the end of our already created URL and end up with a URL that looks as such: https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&&date=2008-08-11
Now we are all set. We have our base URL, our api_key parameter and our date parameter. Let’s go ahead and click send and initiate our GET request.
You should now see the following at the bottom of the window:
There is actually a lot of information in this little window. You can see at the top right the status of 200. The HTTP 200 OK success status response code indicates that the request has succeeded. Next you will see the information that we were looking for as the body of the response. This response returned the date, explanation, hdurl for the image, the media type, title and url for non hd image. If you were following along you should have a similar response based on the date you chose. Now that you have a basic understanding, feel free to change the date or look into the documentation of the NASA API and add other parameters they may offer for this API or try one of their other API’s. These can be found at https://api.nasa.gov/ I hope you enjoyed this brief tutorial on one of my favorite developer tools and now have a better understanding of Postman. There is much more that can be done through this tool and I encourage you to read official documentation and explore further.
[…] you follow our blog regularly, you will know a while back we wrote this one on API Test with Postman. While we won’t knock Postman or it’s slighlty more lightweight counterpart Insomnia, […]