{"id":3282,"date":"2024-05-28T21:28:15","date_gmt":"2024-05-28T19:28:15","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3282"},"modified":"2024-05-28T21:28:15","modified_gmt":"2024-05-28T19:28:15","slug":"rest-client-extension-how-to-send-http-request-from-visual-studio-code","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/05\/28\/rest-client-extension-how-to-send-http-request-from-visual-studio-code\/","title":{"rendered":"REST Client Extension : How to send HTTP request from Visual Studio Code"},"content":{"rendered":"\n<p>As a developer, it&#8217;s important to have efficient tools for testing and debugging APIs. Alongside well-known tools like <strong><em>Postman<\/em><\/strong> or <em>Insomnia<\/em>, Visual Studio Code offers a useful extension called &#8220;<strong><em>REST Client<\/em><\/strong>&#8221; that provides some interesting advantages.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Integrated Tool<\/span><\/h5>\n\n\n\n<p>The biggest benefit of the REST Client extension is that it is integrated directly into Visual Studio Code. This means that we can create and test our API requests right within our code editor, without having to switch between different applications. This increases efficiency and productivity, as we have everything in one place.<\/p>\n\n\n\n<p id=\"1056\">before you were probably using some tool like\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.postman.com\/\" target=\"_blank\"><strong>Postman\u00a0<\/strong><\/a>or\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/insomnia.rest\/\" target=\"_blank\"><strong>Insomnia<\/strong><\/a>. But that mean you should leave your Visual Studio Code instance and\u00a0<strong>open another tool<\/strong>, install it if you do not have it, configure,\u00a0<strong><em>SO TIME WASTING AND CONSUMING<\/em><\/strong>.<\/p>\n\n\n\n<p>Now, we can just\u00a0<strong>install extension REST Client<\/strong>\u00a0inside our\u00a0<strong>VS Code<\/strong>\u00a0and then we\u00a0<strong>can define our HTTP request within .http file<\/strong>\u00a0and test our code without using some other external code.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Syntax Highlighting and Autocomplete<\/span><\/h5>\n\n\n\n<p>The REST Client extension provides syntax highlighting for creating API requests. This makes the code more readable and organized. Additionally, the extension supports autocomplete, which significantly simplifies the process of writing API requests.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Local Storage of Requests<\/span><\/h5>\n\n\n\n<p>Another useful feature is the ability to store API requests locally in our work environment. This way, we can easily retrieve and reuse our frequently used requests, without having to rewrite them every time.<\/p>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Easy API Testing<\/span><\/h5>\n\n\n\n<p>With the REST Client extension, we can test API requests directly from our code editor. We only need to create a .<strong><em>http file <\/em><\/strong>and define our requests within it. Then, we can execute the requests with a single click and view the results right in the editor.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Usage Examples<\/h4>\n\n\n\n<p>Let&#8221;s go to our Extensions in VS Code and install it or download it from here:\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=humao.rest-client\" target=\"_blank\">https:\/\/marketplace.visualstudio.com\/items?itemName=humao.rest-client<\/a><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"606\" height=\"243\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-4.png\" alt=\"\" class=\"wp-image-3283\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-4.png 606w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-4-300x120.png 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure><\/div>\n\n\n\n<p>Now to show you how it works we will create simple\u00a0<strong>Express.js<\/strong>\u00a0endpoint and test it directly from VS Code.<\/p>\n\n\n\n<p>So we will have one file,\u00a0<strong>index.js<\/strong>\u00a0and this in memory list and one GET endpoint.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"515\" height=\"286\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-5.png\" alt=\"\" class=\"wp-image-3284\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-5.png 515w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-5-300x167.png 300w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/figure><\/div>\n\n\n\n<p>Now we should create some file with .http extension. So we will create <strong><em>course.http file <\/em><\/strong>and we will write just two lines of code to test our GET method.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"404\" height=\"142\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-6.png\" alt=\"\" class=\"wp-image-3285\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-6.png 404w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-6-300x105.png 300w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>PS: <\/strong><em>replace the URL with your localhost address.<\/em><\/p>\n\n\n\n<p>Now to test this, we can run our application trought the command :  <strong><em>node index.js<\/em><\/strong><\/p>\n\n\n\n<p>And while our backend is spinning, we can test our endpoint directly from .http file. <\/p>\n\n\n\n<p>As you can see if we have installed <strong><em>REST client <\/em><\/strong>extension, in <strong><em>.http file<\/em><\/strong> we can see button\u00a0<strong><em>Send Request<\/em><\/strong>.<\/p>\n\n\n\n<p>If we execute this action, we should get following output in VS Code.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-7.png\" alt=\"\" class=\"wp-image-3286\" width=\"667\" height=\"319\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-7.png 667w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-7-300x143.png 300w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><\/figure><\/div>\n\n\n\n<p>And that&#8217;s it, simple, isn\u2019t it.\u00a0<strong>Without leaving our favorite IDE<\/strong>, just to see if everything is working as expected.<\/p>\n\n\n\n<p>We can also add a couple of more request. if we want (for example) to test also some POST request, our .http file can look something like this.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"446\" height=\"337\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-8.png\" alt=\"\" class=\"wp-image-3287\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-8.png 446w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2024\/05\/image-8-300x227.png 300w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-luminous-vivid-orange-color\">Summary<\/span><\/h4>\n\n\n\n<h5>1- Sending a GET request to an API:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>GET https:\/\/api.example.com\/users\r<\/code><\/pre>\n\n\n\n<h5>2-Sending a POST request with JSON data:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>POST https:\/\/api.example.com\/users\r\nContent-Type: application\/json\r\n\r\n{\r\n    \"name\": \"John Doe\",\r\n    \"email\": \"john.doe@example.com\"\r\n}\r<\/code><\/pre>\n\n\n\n<h5>3- Authentication with an API key:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>GET https:\/\/api.example.com\/data\r\nX-API-Key: your_api_key_here\r<\/code><\/pre>\n\n\n\n<h5>4-Bundling multiple requests in a single file:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code># Get user list\r\nGET https:\/\/api.example.com\/users\r\n\r\n###\r\n\r\n# Create a new user\r\nPOST https:\/\/api.example.com\/users\r\nContent-Type: application\/json\r\n\r\n{\r\n    \"name\": \"Jane Doe\",\r\n    \"email\": \"jane.doe@example.com\"\r\n}\r\n<\/code><\/pre>\n\n\n\n<p>PS: You can find more details in documentation of this\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=humao.rest-client&amp;ssr=false#overview\" target=\"_blank\"><strong>REST Client library<\/strong><\/a>,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a developer, it&#8217;s important to have efficient tools for testing and debugging APIs. Alongside well-known tools like Postman or Insomnia, Visual Studio Code offers a useful extension called &#8220;REST Client&#8221; that provides some interesting advantages. Integrated Tool The biggest benefit of the REST Client extension is that it is integrated directly into Visual Studio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,620],"tags":[860,300,859,297,52,299],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3282"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3282"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3282\/revisions"}],"predecessor-version":[{"id":3288,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3282\/revisions\/3288"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/2307"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}