Calling REST APIs From the IDE
Published Feb 09 2022 03:36 PM 17.1K Views
Microsoft

Developers use a lot of URLs: your app’s URLs, URLs for consumed services, etc. These URLs, and sometimes their payloads may differ per environment. And as a dev you’re going to constantly hit them. Developers have relied on utilities outside the IDE to track the URLs, send test payloads to the URLs, and inspect the  response. Now you can do all of that from your IDE! To do this we’ll use the REST Client extension for Visual Studio Code.

 

Get Started

  • Click here to Install the extension.
  • Make our first call.
  • Create a new file called client.rest with this content

 

 

 

### First Test
GET http://microsoft.com HTTP/1.1

 

 

 

As soon as you save, you’ll see a Send Request button appear right after the ###.

SameerDoshi_1-1644444713008.png

 

Click send request and you’ll see the response right in the IDE.

SameerDoshi_2-1644444713015.png

 

That’s it! No need to open a web browser, debug tools, networking, and then inspect responses- it’s all right there!

 

If you want to share the response - you can save, copy/paste, or explore the response at your leisure! 

 

Want to share everything you’ve done so far? Simply add the client.rest file to source control and anyone who opens it will run the same call as you just did. But, of course, we’re just getting started!

 

POST Requests With JSON Payload

We can add as many URLs to this file, or we can create other files with a .rest extension.  For now let’s add to our existing file.  Multiple calls are separated by a new line starting with ###

 

Add a post request by adding a ### to the bottom of the file..  Just add ### to add additional requests.

 

As you’d expect you can use <ctrl>+ space to autocomplete all entries. 

 

 

 

###
POST https://localhost:5001/api/Patient HTTP 1.1
Content-Type:  application/json

{

    "name":"H. Simpson"

}

 

 

 

 

Click Send Request and you’ll get a response. You can imagine having a file called services.rest that describes the payload and urls for each of your dependent services, and another file called app.rest that describes the URLs and payloads for your app’s URLs! But if we have many URLs we might want to change the host depending on the environment.

 

Variables

You can add variables in the same client.rest file using the syntax

@<variable name> = <variable value>

 

Then you can place the variable by using {{variable name}} anywhere in the rest of the file.

If you want the variable to refer to a specific request simply put the variable declaration inside the ###.

 

You can also store variables at the environment level (esp. useful if you want to avoid checking in secrets to source control).

 

 

 

@host=microsoft.com

### Get Base

GET https://{{host}}

### Post Base

@patientid=1234

POST https://localhost:5001/api/Patient/

Content-type: application/json

{

   Id:{{patientid}}

}

 

 

 

FHIR Sample

This is great for development but it makes developing against FHIR endpoints incredibly easy.

Take a look at this example

 

 

 

### Get single patient

@patientid=f5e7ee94-bfc6-465e-a64d-f0c90e3d50f5

GET https://{{fhirname}}.azurehealthcareapis.com/Patient/{{patientid}}

Authorization: Bearer {{auth.response.body.access_token}}

 

 

 

This gets a single patient resource. Why is this better than a Postman collection? You can easily check it into source, you can easily edit it without having to click around the Postman GUI, and most importantly- you can quickly copy paste the request or response since it’s all text.

 

Please see the example client.rest included in the Open Hack for FHIR.

This example includes queries for getting a token, fetching a patient, and fetching a list of patients!

 

Benefits

Using the REST Client has many benefits. But chief among them is speed. If you use an external tool, you’ll have to alt-tab to it while you develop. You’ll have to be trained, and train others on it. With REST Client- you only need to know how to use the same IDE you already know and love!

  • Stay in the IDE – stop the context switching. Dev and test from the same place.
  • Document All URLs your app uses  - No more bookmarks or comments in code- just document the URLs as you dev
  • Store URLs next to your code – Since everything is in simple plain text files, as you change and modify your .rest files you’ll be able to easily track the differences.
  • Quickly Test URLs – a year after you stop working on a project- you simply open the text file and hit “send request” to send a request. No more importing and setting up an environment.
  • Inspect Responses As Text Files – use all the tools of VS Code  to look through the response: collapse/expand, find, regex.
  • Save/Share Responses – No more screenshots of browser dev tools- simply send others the entire response payload.
  • Code Spaces Compliant

Postman still has features that you won’t find anywhere else, but for most day-to-day app dev use cases you’ll  find REST client extension in Visual Studio Code meets your needs.

1 Comment
Version history
Last update:
‎Feb 09 2022 03:31 PM
Updated by: