Query Params & Path Variables

Query Params & Path Variables

Objectives

  • What Query Params is
  • How to properly input the key and value pair in POSTman to do a filter Request
  • Learn what a Path Variable is

APIs typically have optional query parameters. Query parameters can be defined as the key-value pairs that appear after the question mark ? in the URL.

Let’s say we want to get only the posts of a user with an id of 1. We can do that in Postman under the query parameters.

Under the Params tab, you can see a KEY and VALUE column. You can specify which KEY you are going to use (in this case userId), and which VALUE (in this case 1). You should also notice that the URL we manually typed in in the request input box has been updating automatically (based on the key-value pairs we specified in Query Params).

Clicking on Send will return a response that will filter through all the posts and will only get the posts created by userId 1.

Path Variables

For endpoints that change all the time, like the id number of a user, or the id number of a post, we can use Path Variables.

The syntax for using path variables in Postman is to use the colon sign : and then the name of the variable. Example:

/posts/:postid

Where in postid is the variable name for the path we want to get resources from. Postman will notice that you have a Path variable in your URL and will show you the Path Variables table below the Query Params table.

Under KEY is postid and this key name can be whatever you want. Typically this will be named as id. The VALUE should be the id number of the post (Put 1 as value in this example). Unlike Query Params, Path Variables don’t put key-value pairs in the URL. Instead of :postid in the URL, :postid will actually be replaced with whatever value you put in the VALUE column under Path Variables.

Next