Blog Post

How to Call REST API using VS Code

,

Sometimes you need a fast way to test your API call and responses, and for that VS code is the perfect tool to do that. For today’s demo, we will use a free API to test with.

https://catfact.ninja/fact
Keep in mind, this is a very very simple REST API. For more complicated REST API calls, make sure to refer to the documentation around the API itself to know the required and optional parameters.

To start, we will need to install a REST API extension in Visual Studio Code. To find extensions, navigate to the block-looking icon and search for “rest” in the search bar. When picking an extension, I recommend picking one with a lot of installs and high star reviews. For our demo, I’m going to install the first option – “REST client”.

Now that we have a REST extension, we can build a rest file to run our code in. To set up a file to run HTTP requests (aka REST API calls), we need to make a text file with the extension “.http” or “.rest”. To do this, create a new file in VS Code and save it as “api_demo.rest”. You can name it whatever you’d like, but the key here is to have .rest a the end.

Make sure the file extension is set to “No Extension” so that the .rest can change the text file to a rest file. Now that we are inside a rest enabled file, we can build out our API call. There are a few standard REST API call types such as GET and POST. For this API, we will use GET to get information. It’s typically as simple as it sounds, but definitely triple check documentation if you’re unsure what call to use.

Add the following code to your file: GET https://catfact.ninja/fact

The GET should be colored and there should be a small subscript that allows you to Send the Request.

Select Send Request, and boom! You’ve run your first REST API! The results will pop up on the right. Happy coding!

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating