avatarJerry Ng

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4139

Abstract

aining Requests</h1><p id="6b34">Insomnia provides the ability to extract values from the responses of other requests (<a href="https://support.insomnia.rest/article/175-chaining-requests">source</a>).</p><p id="4aad">Imagine if we want to run the<code>createArticle</code> mutation to create a new article, the server would require us to be authorized first.</p><p id="a15a">In other words, an access token of some sort would be required in the request header of the <code>createArticle</code> mutation.</p><h2 id="cb12">Without chaining requests, you have to:</h2><ol><li>Send a <code>tokenAuth</code> mutation to obtain the <code>token</code>.</li></ol><div id="a547"><pre><span class="hljs-meta"># Mutation</span> mutation TokenAuth(username: <span class="hljs-built_in">String</span>!, password: <span class="hljs-built_in">String</span>!) { tokenAuth(username: username, password: password) { token } }</pre></div><div id="5a90"><pre><span class="hljs-type">#</span> <span class="hljs-built_in">Query</span> <span class="hljs-built_in">Variables</span> <span class="hljs-punctuation">{</span> <span class="hljs-string">"username"</span><span class="hljs-operator">:</span> <span class="hljs-string">"username"</span><span class="hljs-operator">,</span> <span class="hljs-string">"password"</span><span class="hljs-operator">:</span> <span class="hljs-string">"password123"</span> <span class="hljs-punctuation">}</span></pre></div><div id="0183"><pre><span class="hljs-attr"># Response { "data": { "tokenAuth": { "token": "eyJhbGciOiJIUzI1</span><span class="hljs-symbol">NiIsInR5</span>cCI<span class="hljs-number">6</span>IkpX<span class="hljs-attr">VCJ9</span>.eyJzdWIiOiIxMj<span class="hljs-name">M0</span><span class="hljs-symbol">NTY3</span>ODkwIiwibmFtZSI<span class="hljs-number">6</span>Ikpva<span class="hljs-name">G4</span>gR<span class="hljs-name">G9</span>lIiwiaWF<span class="hljs-number">0</span>Ijox<span class="hljs-symbol">NTE2</span>Mj<span class="hljs-name">M5</span>MDIyfQ.SflKxwRJSMeKKF<span class="hljs-number">2</span>QT<span class="hljs-number">4</span>fwpMeJf<span class="hljs-number">36</span>POk<span class="hljs-number">6</span>yJV_adQssw<span class="hljs-number">5</span>c<span class="hljs-string">" } } }</span></pre></div><p id="b017">2. Copy the value of <code>token</code> from the <code>data.tokenAuth.token</code> from our response.</p><figure id="8934"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*WULnaOiPyUJcrc9YusGg7g.png"><figcaption>Attaching our <code>token</code> from <code>tokenAuth</code> to our <code>createArticle</code> Authorization header</figcaption></figure><p id="c159">3. Paste the <code>token</code> value (<code>eyJhbGciOiJIUzI1NiI.....</code>)in the authorization header of the <code>createArticle</code> request and hit ‘Send’.</p><p id="0c3b">4. Repeat Step 1 — 3 every time the <code>token</code> expires.</p><h2 id="1186">Steps with chaining requests:</h2><figure id="2242"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*iZ-1mtodSov9jyo1-pdNrw.png"><figcaption>Select Response → Body Attribute</figcaption></figure><figure id="afc8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*73I7TxkLj09Dh6wOxC_98w.png"><figcaption>Select and fill up accordingly</figcaption></figure><ol><li>Setup <code>tokenAuth</code> mutation once as Step 1 above.</li><li>Go to <code>createArticle</code>, under the “Bearer” option, select “Bearer Token”</li><li>At “TOKEN”, hit <code>Control/Cmd + Space</code>, select “Response → Body Attribute”</li><li>At “Edit Tag”, under “Request”, select the request that you have set up in Step 1 (i.e. <code>POST</code> <code>tokenAuth</code>)</li><li>Under “Filter”, filter your response by JSONPath i.e. <code>$.data.tokenAuth.token</code></li><li>Lastly, you may configure the “Trigger Behaviour” accordingly</li><li>Hit ‘Send’ at your <code>createArticle</code> mutation</li></ol><p id="14cf">The idea here is that instead of having to copy the value of <code>token</code> manually <b>every time</b> from the response to <code>createArticle</code> mutation

Options

, we would only perform Step 1 — 6 <b>once</b>.</p><h1 id="82ee">Plugins</h1><p id="80f4">One of my favorite reasons for using Insomnia is that it supports numerous plugins that can be used to extend the functionality of Insomnia.</p><p id="ba91">To install any plugin, simply hit <code>Control/Cmd + ,</code> key on your keyboard, go to ‘Plugin’, and type in the <code>npm-package-name</code> of the plugin that you want to install on Insomnia.</p><p id="d0f5">You may find a list of available plugins <a href="https://insomnia.rest/plugins/">here</a>.</p><h2 id="04a0">Faker Plugin (insomnia-plugin-faker)</h2><figure id="6631"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8gq9ThasWiWjlSTXlScrWw.png"><figcaption>Simply hit <code>Control/Cmd + Space</code> to search <i>Faker inputs</i></figcaption></figure><p id="c525">If you come from the JavaScript world, you probably have heard of <a href="https://github.com/marak/Faker.js/">Faker</a>. In short, one would use Faker to generate a massive amount of realistic fake data.</p><p id="bd82">The <a href="https://github.com/bbbco/insomnia-plugin-faker"><code>insomnia-plugin-fa</code>ker</a> allows us to generate Faker data right within the Insomnia itself which can be super handy.</p><p id="9284">As an example, under query variables for your <code>createArticle</code> mutation, simply hit <code>Control/Cmd + Space</code> and type <code>faker</code> so that the autocomplete can render the type of Faker data you want to generate.</p><p id="d021">Simple as that.</p><h2 id="e8d0">Gist Sync Plugin (insomnia-plugin-gist-sync)</h2><p id="f2b4">Another amazing plugin that I use a lot personally is the <a href="https://insomnia.rest/plugins/insomnia-plugin-gist-sync"><code>insomnia-plugin-gist-s</code>ync</a> plugin.</p><p id="a729">The plugin allows users to sync workspaces with GitHub gist, for free! Simply just follow the steps in the link given to set up.</p><p id="dbf9">I’d highly recommend you to check out their paid <a href="https://insomnia.rest/pricing">version</a> of the app as they come with additional features that allow for better team collaboration.</p><h1 id="d6d1">Closing Thoughts</h1><p id="4112">Prior to using Insomnia, I was using Postman a lot. Eventually, I find myself using Insomnia more and more.</p><p id="a0ff">Personally, I find Insomnia very easy to work with. Not forgetting to mention that I simply just like Insomnia’s <b>cleaner</b> and <b>less cluttered UI</b>. The hotkeys on Insomnia are much more intuitive to use too.</p><p id="17e8">Finally, Insomnia supports the ability to import multiple file types such as Postman v2, OpenAPI, Swagger, and <a href="https://support.insomnia.rest/article/52-importing-and-exporting-data">more</a>.</p><h2 id="4e0a">Originally published at jerrynsh.com</h2><div id="6d00" class="link-block"> <a href="https://betterprogramming.pub/how-to-write-clean-code-in-python-5d67746133f2"> <div> <div> <h2>How To Write Clean Code in Python</h2> <div><h3>3 Tips for writing clean code with Python examples</h3></div> <div><p>betterprogramming.pub</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*S7l8m1iFRMJve25V)"></div> </div> </div> </a> </div><div id="7fef" class="link-block"> <a href="https://readmedium.com/top-3-backend-developer-productivity-tools-that-you-should-check-out-bc16bff11133"> <div> <div> <h2>Top 3 Backend Developer Productivity Tools That You Should Check Out</h2> <div><h3>Read About Tools That I Use to Increase My Productivity as a Backend Developer in 5 Minutes</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*55Z7N5x4DVyJpnxt)"></div> </div> </div> </a> </div></article></body>

Fast Track Your API Development With Insomnia

A fast yet simple alternative to Postman for sending REST and GraphQL requests

Photo Credits: Insomnia REST Client

If you are developing a set of APIs, chances are you already know Postman — an API Client for API development. Generally, one would use an API client to test APIs in terms of functionality, performance, security, reliability, and the correctness of the responses.

While Postman is the de facto standard for API testing and development, I would like to draw your attention to a fast and easy-to-use alternative to Postman — Insomnia REST Client.

The goal of this article is to share with you some of the handy tips and tricks that you can use with Insomnia, especially while developing GraphQL APIs, though it works great with REST too.

If you want to try out the APIs used in this article, feel free to check out:

Why Not Postman?

No Support for Schema Import via URL and Autocomplete

With the introspection system, GraphQL is able to provide a way for clients to discover the resources that are available in a GraphQL schema which then allows features like self-documentation and autocompletion to be used by the users.

Postman as of the date of writing this article does not directly support importing GraphQL schemas via URL directly (issue). As a result, to use autocomplete on Postman, one would need to import your schema file manually to Postman every time there are new changes made to your schema which can be very cumbersome.

Here are some useful tips when using Insomnia.

Environment Variables

Hotkey: Cmd/Ctrl + E
If the hotkey Control + Space doesn’t work, use {{ENV_VAR_NAME}} instead

Like Postman, we could easily set Environment Variables

in Insomnia and use them later on when making our requests.

For instance, we can configure our API’s BASE_URL according to our environment such as Local, Staging or Production and switch to a different environment with ease.

Do note that the environment is to be filled with JSON format.

To reference any environment variable, simply press Control/Cmd + Space on your keyboard.

NOTE: In case the autocomplete does not render, simply use{{ENV_VAR_NAME}} instead.

Chaining Requests

Insomnia provides the ability to extract values from the responses of other requests (source).

Imagine if we want to run thecreateArticle mutation to create a new article, the server would require us to be authorized first.

In other words, an access token of some sort would be required in the request header of the createArticle mutation.

Without chaining requests, you have to:

  1. Send a tokenAuth mutation to obtain the token.
# Mutation
mutation TokenAuth($username: String!, $password: String!) {
  tokenAuth(username: $username, password: $password) {
    token
  }
}
# Query Variables
{
 "username": "username",
 "password": "password123"
}
# Response
{
  "data": {
    "tokenAuth": {
      "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
    }
  }
}

2. Copy the value of token from the data.tokenAuth.token from our response.

Attaching our token from tokenAuth to our createArticle Authorization header

3. Paste the token value (eyJhbGciOiJIUzI1NiI.....)in the authorization header of the createArticle request and hit ‘Send’.

4. Repeat Step 1 — 3 every time the token expires.

Steps with chaining requests:

Select Response → Body Attribute
Select and fill up accordingly
  1. Setup tokenAuth mutation once as Step 1 above.
  2. Go to createArticle, under the “Bearer” option, select “Bearer Token”
  3. At “TOKEN”, hit Control/Cmd + Space, select “Response → Body Attribute”
  4. At “Edit Tag”, under “Request”, select the request that you have set up in Step 1 (i.e. POST tokenAuth)
  5. Under “Filter”, filter your response by JSONPath i.e. $.data.tokenAuth.token
  6. Lastly, you may configure the “Trigger Behaviour” accordingly
  7. Hit ‘Send’ at your createArticle mutation

The idea here is that instead of having to copy the value of token manually every time from the response to createArticle mutation, we would only perform Step 1 — 6 once.

Plugins

One of my favorite reasons for using Insomnia is that it supports numerous plugins that can be used to extend the functionality of Insomnia.

To install any plugin, simply hit Control/Cmd + , key on your keyboard, go to ‘Plugin’, and type in the npm-package-name of the plugin that you want to install on Insomnia.

You may find a list of available plugins here.

Faker Plugin (insomnia-plugin-faker)

Simply hit Control/Cmd + Space to search Faker inputs

If you come from the JavaScript world, you probably have heard of Faker. In short, one would use Faker to generate a massive amount of realistic fake data.

The insomnia-plugin-faker allows us to generate Faker data right within the Insomnia itself which can be super handy.

As an example, under query variables for your createArticle mutation, simply hit Control/Cmd + Space and type faker so that the autocomplete can render the type of Faker data you want to generate.

Simple as that.

Gist Sync Plugin (insomnia-plugin-gist-sync)

Another amazing plugin that I use a lot personally is the insomnia-plugin-gist-sync plugin.

The plugin allows users to sync workspaces with GitHub gist, for free! Simply just follow the steps in the link given to set up.

I’d highly recommend you to check out their paid version of the app as they come with additional features that allow for better team collaboration.

Closing Thoughts

Prior to using Insomnia, I was using Postman a lot. Eventually, I find myself using Insomnia more and more.

Personally, I find Insomnia very easy to work with. Not forgetting to mention that I simply just like Insomnia’s cleaner and less cluttered UI. The hotkeys on Insomnia are much more intuitive to use too.

Finally, Insomnia supports the ability to import multiple file types such as Postman v2, OpenAPI, Swagger, and more.

Originally published at jerrynsh.com

API
Development
GraphQL
Rest Api
Testing
Recommended from ReadMedium