Testing in Postman with ON API

  • Introduction

    There are a variety of tools for testing API's, but in this tutorial we'll focus on the free multi-platform app called Postman.

    It offers a variety of ways to easily and efficiently test different API endpoints that you'd like to call. It's also a great way to troubleshoot problems you experience while using an API, because you can click one button to run all your API calls and see what statuses they return.

    If you follow the steps below, you'll no longer need to copy and paste the token you receive during the authentication step. This process will streamline your workflow and enable you to focus on the API.

  • Step 1 – Install and create a collection

    Install the app

    Visit the Postman website and download the free app for your operating system.

    Once installed, open Postman and continue this tutorial.

    Create a collection

    1. From the menu, click Collection, New Collection.

    2. Enter a name.

    3. Select Create.

  • Step 2 – Set up an environment

    1. In the upper right corner, select the Settings gear.

    2. Select Manage Environments.
      part 1-2

    3. Select Add.

    4. Enter an environment name, such as the name of your school.

    5. Add a key named base and fill in your school URL with /api at the end. For example, https://{school}.myschoolapp.com/api.

    6. Add a second key named token and enter **token** for the value. We enter a temporary value here for the key to get saved.

    7. Add a third key named user and enter the user name for your Web API Services Manager user.

    8. Add a fourth key named pass and enter the password for your Web API Services Manager user.

    9. Click Add.
      part 1-3

  • Step 3 – Add authentication

    Set up the call

    In Postman, use double curly braces {{ }} to signify variables that will be replaced by the environment we're testing in.

    1. Open a new tab.

    2. Select GET.

    3. To perform the authentication step, copy the following URL:

      {{base}}/authentication/login/?username={{user}}&password={{pass}}

    4. Paste into the Enter request URL field.

    5. Select Save. The Save Request screen appears.

    6. The Request Name defaults based on the Enter request URL field. Choose the Collection you created in the first step.

    7. Select Save.
      authentication

  • Step 4 – Save the token

    To be able to reuse the token in future calls, you must complete this step.

    1. In the new tab you created in Step 3, select Tests.

    2. Copy the code provided below:

      var jsonData = JSON.parse(responseBody);
      postman.setEnvironmentVariable("token", jsonData.Token);
      tests["Status code is 200"] = responseCode.code === 200;
      

    3. Paste into Tests.
      Tests

    4. Select Save.

    5. Select Send.

    6. If the response was successful (Status: 200 OK), in the Body your token should appear.
      Success Token
      This token will be stored in our token variable and used in the next step.

  • Step 5 – Run a GET test

    1. To create a new tab, select the plus button next to your current tab.

    2. Copy the following code and paste into the Enter request URL field.

      {{base}}/schoolinfo/schoollevel?t={{token}}
      

    3. Copy the following code and paste under Tests.

      tests["Status code is 200"] = responseCode.code === 200;
      We recommend you put this in all your requests. When you run everything in your collection, you can quickly see which API endpoints succeeded.
      Once you create this request, you can quickly duplicate it.

    4. Select Save and add it to your collection.

    5. Select Send.

    6. If the call sends successfully, you should see a list of the school levels at your organization.
      school level call

  • Step 6 – Add multiple parameters

    In Postman, you can easily add parameters and values without getting lost in a long URL.

    1. Select the Params button.

    2. Add keys, with corresponding values, for each paramter.
      parameters

    Tip: These parameters can be saved variables in your environment, or values you type in.

  • Step 7 – Confirm your runner

    After you have multiple requests created and saved, make sure your Authentication request is the first one in your list. It's safest to do this with GETs.

    1. In the top left, select Runner.

    2. Under Choose a collection or folder, select your collection.
      collection runner

    3. Select your environment.

    4. Select Start Run. Your collection runs and you'll be able to see which of your calls were successful.
      run results

    5. Confirm that the first call was a GET of your authentication token.

  • Step 8 – Run a PUT or POST

    Warning: If you set up your live school environment in Step 2, then any PUT or POST or DELETE calls will be directly affecting your live school data.

    To do a PUT or POST, you need to set the Headers and the Body in addition to setting the method.

    1. Choose the appropriate method, PUT or POST.

    2. Add a JSON payload to Body, select raw.
      put or post

    3. Under Headers, enter Content-Type for the key and a value of application/json.
      put or post headers

  • Step 9 – Next steps

    Get starter code snippets

    Do you want a jump start on some code to get started with in your custom app? If yes, select the Code link and choose the language of your choice. Postman will convert your request into a code snippet that you can try out in your project!

    Helpful?

    Was this tutorial helpful? We welcome your feedback below.