<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Beatrice Schembri</title>
    <description>The latest articles on DEV Community by Beatrice Schembri (@biceschembri).</description>
    <link>https://dev.to/biceschembri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1103824%2F3de6e874-b99d-40dc-8ec1-922737b29a26.jpeg</url>
      <title>DEV Community: Beatrice Schembri</title>
      <link>https://dev.to/biceschembri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/biceschembri"/>
    <language>en</language>
    <item>
      <title>How I used Postman to test my Express API</title>
      <dc:creator>Beatrice Schembri</dc:creator>
      <pubDate>Sun, 18 Jun 2023 21:16:35 +0000</pubDate>
      <link>https://dev.to/biceschembri/how-i-used-postman-to-test-my-express-api-1bk0</link>
      <guid>https://dev.to/biceschembri/how-i-used-postman-to-test-my-express-api-1bk0</guid>
      <description>&lt;p&gt;In this article I will show how I used the &lt;strong&gt;Postman&lt;/strong&gt; platform to test and develop a simple API project built in Express.&lt;/p&gt;

&lt;p&gt;There are many resources you can find online on the subject, and I recommend you check them out for more tips (I will link a few in the References section at the end). For a systematic approach, you can start from the very own &lt;a href="https://learning.postman.com/docs/introduction/overview/"&gt;Postman documentation&lt;/a&gt;. Here I will show my personal experience using Postman, with some beginner-friendly steps - after all, this was a beginner's project as well.&lt;/p&gt;




&lt;h4&gt;
  
  
  Table of Contents
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;What is Postman&lt;/li&gt;
&lt;li&gt;Setting up Postman&lt;/li&gt;
&lt;li&gt;
Using Postman

&lt;ul&gt;
&lt;li&gt;Requests&lt;/li&gt;
&lt;li&gt;Collections&lt;/li&gt;
&lt;li&gt;Authorization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;Further reading&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What is Postman
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ju31F5OL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxus3rxtup24nno82t19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ju31F5OL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxus3rxtup24nno82t19.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Postman is an &lt;strong&gt;API platform&lt;/strong&gt; that developers can use to build, test and use their APIs. It provides a wide range of functionalities, and just to mention a few: creating workflows, setting up team collaboration workspaces, configuring governance and security rules, and generating reports.&lt;/p&gt;

&lt;p&gt;There are a few different pricing plans depending on the size of your enterprise. The good news is, Postman is &lt;a href="https://learning.postman.com/docs/collaborating-in-postman/working-with-your-team/collaboration-overview/#:~:text=in%20this%20section.-,Postman%20Free%20lets%20teams%20of%20up%20to%20three%20work%20together,in%20the%20upper%2Dright%20corner."&gt;free to use&lt;/a&gt; for developers not working with a team, or in a team of up to three people. So, a great starting place for solo projects like mine.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setting up Postman
&lt;/h2&gt;

&lt;p&gt;Go and &lt;a href="https://identity.getpostman.com/signup"&gt;register&lt;/a&gt; a Postman account if you don't have one already. Done? Great! Now you can start using Postman.&lt;/p&gt;

&lt;p&gt;You can launch the platform from your web browser, but bear in mind that it comes with certain limitations, like not being able to use the &lt;em&gt;Find and replace&lt;/em&gt; method to find and replace values in a workspace.&lt;/p&gt;

&lt;p&gt;It is therefore recommended, whenever you can, to download the latest Postman version and using it directly on your machine. All the steps can be found at &lt;a href="https://learning.postman.com/docs/getting-started/installation-and-updates/#using-the-postman-web-app"&gt;this link&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using Postman
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NypEU0Ia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdo5l6f5k2fioxsdqq44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NypEU0Ia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdo5l6f5k2fioxsdqq44.png" alt="Image description" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My project consists of a collection with CRUD logic for the items, an authentication system that uses JWT tokens, and a posts and comments system. &lt;a href="https://github.com/BiceSchembri/express-API-tattoo"&gt;Here&lt;/a&gt; you can view the Github repo.&lt;/p&gt;

&lt;p&gt;The project was built with the &lt;strong&gt;Express&lt;/strong&gt; framework for &lt;strong&gt;Node.js&lt;/strong&gt;, together with a SQL database. The aim of the assignment was to build and test the back-end side only, meaning the whole user interface was missing. That is why I used Postman to process the requests to read, create, update, and delete items from my collection.&lt;/p&gt;

&lt;p&gt;To start, I created a new &lt;strong&gt;Workspace&lt;/strong&gt;. You can have multiple workspaces in your account, both personal or shared between team members. Then create a new Collection bu clicking on the &lt;strong&gt;+&lt;/strong&gt; sign on your Collections tab, under your Workspace name, and give it a meaningful name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collections
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_CbUfQfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2jiaffqxzfkiu8qolsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_CbUfQfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2jiaffqxzfkiu8qolsa.png" alt="Image description" width="787" height="1002"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Collections help keep your requests organized, not only for better readability but also for testing purposes. In my Postman workspace, I wanted to follow the same structure and logic as my project repository - meaning requests were divided into&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Profile&lt;/strong&gt; - endpoints for the user profile / dashboard&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session&lt;/strong&gt; - register, login and logout routes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Products&lt;/strong&gt; - CRUD routes for the collection items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Posts&lt;/strong&gt; - CRUD for new posts published by users&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comments&lt;/strong&gt; - CRUD for comments under posts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Admin&lt;/strong&gt; - endpoints accessible by admins only, e.g. to delete users&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Landing page&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Requests
&lt;/h3&gt;

&lt;p&gt;Now I could add requests to my collections.&lt;/p&gt;

&lt;p&gt;As a refresher, these are the different HTTP requests:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HTTP Method&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;td&gt;Retrieves a representation of a resource or resource(s).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;td&gt;Submits data to be processed to create a new resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;td&gt;Updates or replaces an existing resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH&lt;/td&gt;
&lt;td&gt;Partially updates an existing resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Removes a specified resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HEAD&lt;/td&gt;
&lt;td&gt;Retrieves only the response headers for a resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OPTIONS&lt;/td&gt;
&lt;td&gt;Returns the HTTP methods supported by a resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;And here are the steps to add them in Postman.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a new request&lt;/strong&gt; by clicking on the &lt;strong&gt;+&lt;/strong&gt; sign at the top of your workspace overview.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Specify the HTTP method&lt;/strong&gt;. The default request is GET, but you can select the type you need from the drop-down menu.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Enter the URL&lt;/strong&gt; in the appropriate field (in my case, the localhost path followed by the correct URI). Remember to set the appropriate request URL, including any query parameters or route parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add a body&lt;/strong&gt; if it is a POST/PUT/PATCH request. The body is the data that is sent to the API endpoint, so you will need it if you are creating or updating a resource. DELETE generally does not require a body, nor does a GET request, since it is only retrieving data from the server. Typically, you will specify &lt;code&gt;raw&lt;/code&gt; mode and select the &lt;code&gt;JSON&lt;/code&gt; format for your body data, which will be written in curly brackets.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5CvqkqGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuqlozcgrhtvhlqrqkga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5CvqkqGk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuqlozcgrhtvhlqrqkga.png" alt="Image description" width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Anything else?&lt;/strong&gt; You might want to specify particular headers to send along with requests (aka additional metadata). This was not the case for my project - as I said, I kept things quite simple!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hit 'Send' to test it.&lt;/strong&gt; Now you can pay attention to the &lt;a href="https://learning.postman.com/docs/sending-requests/responses/"&gt;Response&lt;/a&gt; you get, the Status, the time and size. You can save the response as Example as well. Keep reading to see different responses depending on the auth privileges.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Name your request and hit 'Save'&lt;/strong&gt; so you will be able to come back to it (you can modify any part of it later on, or keep adding new requests).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Authorization
&lt;/h3&gt;

&lt;p&gt;As mentioned above, I added some &lt;strong&gt;authentication and authorization&lt;/strong&gt; to my project as well. I decided to implement my auth/auth using &lt;strong&gt;&lt;a href="https://jwt.io/"&gt;JWT tokens&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;TIP!&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Check out &lt;a href="https://dev.to/rallofield/auth-auth-auth-2h3m"&gt;this article&lt;/a&gt; for a handy guide on &lt;strong&gt;using middlewares in Express&lt;/strong&gt;, specifically for JWT authentication, and how to test them with Postman.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That meant I couldn't simply send any request via Postman and expect to receive a response - for certain routes and requests, I had to have access rights as user or admin. Only logged in users could publish posts or comments, or view and modify their own personal data; only admins could delete users, and so on.&lt;/p&gt;

&lt;p&gt;A few steps were needed here: let's break them down.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First of all, I had to &lt;strong&gt;obtain a valid JWT token&lt;/strong&gt; by sending a &lt;strong&gt;login request&lt;/strong&gt; via Postman.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;strong&gt;Response&lt;/strong&gt; overview, under the &lt;strong&gt;Headers&lt;/strong&gt; section, I selected and copied the token that can be seen in the &lt;code&gt;Set-Cookie&lt;/code&gt; field.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZK8cK2-n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sesmfbradk5osj5h9iz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZK8cK2-n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sesmfbradk5osj5h9iz7.png" alt="Image description" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I headed over to the &lt;a href="https://jwt.io/"&gt;JWT.io page&lt;/a&gt; and copy-pasted the token obtained from the authentication request to decode it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now I could add some extra configuration in the &lt;strong&gt;Authorization&lt;/strong&gt; section.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_DSYCxxu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gp1czemjx8nut5gdk5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_DSYCxxu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gp1czemjx8nut5gdk5n.png" alt="Image description" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;Type&lt;/strong&gt;, I selected &lt;code&gt;JWT Bearer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;Add JWT token to&lt;/strong&gt;, I selected &lt;code&gt;Request Header&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithm&lt;/strong&gt;: usually, the algorithm used to generate the JWT signature is &lt;code&gt;HS256&lt;/code&gt;. It is the preselected option.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secret&lt;/strong&gt;: This is the JWT access token that I generated and saved in my &lt;code&gt;.env&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Header&lt;/strong&gt; and &lt;strong&gt;Payload&lt;/strong&gt; that were decoded previously could also be added now.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now see the different responses depending on the authentication.&lt;/p&gt;

&lt;p&gt;First, a user who is not authenticated (not registered or not logged in):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EpstmZXK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00bhozwyw45m4dkgnuz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EpstmZXK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00bhozwyw45m4dkgnuz2.png" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then a user who is logged in, but not authorized to access a specific resource:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZF0mJ5Bz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q66ceivgqho7kdtcj28k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZF0mJ5Bz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q66ceivgqho7kdtcj28k.png" alt="Image description" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, a user that is logged in and authorized to perform the request:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mof4sy0Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/823n04xowxsgd2rx32cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mof4sy0Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/823n04xowxsgd2rx32cw.png" alt="Image description" width="800" height="578"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Postman helped me to build and test my pure-backend collection project with a user-friendly interface and clear instructions. Online tutorials and guides were there to help with the trickier configuration bits.&lt;/p&gt;

&lt;p&gt;Although I did not use all the tools that Postman offers to work on my project, what I had was enough to make my work easier to build and to test. More tools are certainly worth exploring - above all, &lt;strong&gt;&lt;a href="https://www.postman.com/automated-testing/"&gt;automated testing&lt;/a&gt;&lt;/strong&gt;, which I hope to implement soon in this project or the next one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;p&gt;I hope this article helped you! It just scratched the surface. Here you can find a few more useful resources to start testing your API with Postman.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://androidgeek.co/how-to-use-postman-to-test-an-api-a8eefdb1d321"&gt;How to Use Postman to Test an API
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/assertqualityassurance/rest-api-test-automation-with-postman-jenkins-1-of-3-860edf3c2a45"&gt;API Testing with Postman — Getting Started
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.zartis.com/api-automation-with-postman-best-practices-by-example/"&gt;API Automation with Postman – Best Practices by Example
&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postman</category>
      <category>postmanapi</category>
      <category>express</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
