What I learnt from attempting to use Postman for a day

https://www.getpostman.com/

Since the beginning of the course I have really struggled with my understanding of the phrase API. I have had this explained multiple times, by many different people, read multiple definitions, and heard the phrase at least 100 times in the last 10 weeks. But the penny did not drop.

Postman bills its self as making API development simple. Which instantly rang alarm bells that I would not 'get' this too.

At this stage in my coding journey I would not be so bold as to say it makes API development simple.

But what it did do for me was help me to get an understanding of how express.js is going to work with REACT within the MERN stack. And I think, made me finally have some comprehension of what an API is.

When completing a tutorial about a MERN stack I read the following:

'I recommend using something to run and test your API. I use Postman'

At this stage I had no idea I was creating an API.

I also had no idea what I wanted the API to be returning.

I did some research and downloaded Postman from the website.

To give some background I had created a get and post route using Express.js as end points to serve our REACT front end.

To give more detail(in an oversimplified way), my get route/method was created to get all the information within the database to pass these JSON objects to REACT to render to screen.

My post method was created to allow REACT to obtain form inputs, and pass these to my post method/route, which would add this information to our database in the format outlined within the model schema we created.

I had not yet created a react front end, so I needed some way to test that these methods were returning the information, and processing the information like I wanted. It was Postman's time to shine.

Postman allows you to make GET request and shows the body response received.

To allow me to test the express.js I first needed to boot up my server.

My express server has been set up to run on port 3001, so I need to make my request to this address.

This returns the body of the request.

What this confirmed:

  • My method returns the JSON I want

But more than that it also gave me some clarity on what I need express to do. I realised I need express to act as a middle man between the front end - REACT - and the back end - my database(Mongo). React needs this JSON data to render to the screen, and it gets this by visiting the express GET route, which returns the JSON body. It also made me understand more about API's, it is just as it sounds. An API is a public interface that allows other 'things' to communicate with it. And it does this through thought out controlled methods.

Process

  1. REACT makes a GET request to our webpage route created by express.js GET method
  2. Express.js makes a request to the database to return the JSON objects
  3. Express.js adds the JSON to the body of the response
  4. React can use the body to render the information from the database to the screen

Postman also allows you to make POST requests and shows the response received.

What this confirmed:

  • My post request does what I want - it can take parameters and add them to the database as per my model

But more than this it also again gave me clarity on the interaction between REACT and express. Postman allowed me to create a mock out the post request I need REACT to do. This gave me more insight on how the interactions will work, and want I want to achieve within REACT. It gave me a better model of the MERN stack as a whole and the 'data journey'.

Process

  1. REACT makes a POST to the route which express.js serves within POST method.
  2. Express.js takes the JSON data and formats it to my Database Schema(Model)
  3. It saves this formatted JSON information as a JSON object to my Database (Mongoose)
  4. The database saves the information as a JSON object within the Database.

Doing these two checks added a lot of clarity, but also meant I could move on to creating the REACT front end safe in the knowledge that my express.js app was sending and processing the information I needed.

I have now developed my web API definition to the follow:

It describes the way two 'things' can communicate with each other, in this scenario with GET and POST requests, and JSON information is exchanged. The things cannot communicate outside the defined methods, only through exposed methods.

I should also add, that on the first 3 attempts I was not getting the output I needed, which helped me work on my express code - but this is a subject of what would be another blog.

p.s I have to add that Postman can do a lot more than I could ever write about, and I have not even scratched the surface!

results matching ""

    No results matching ""