Visual Studio - Http Files

Posted by ryansouthgate on 14 Mar 2023

Intro

Want to run a GET/POST/PUT request from inside Visual Studio 2022? Now you can, using .http files.

These are very simple text files with the extension .http. They allow you to declaratively build a HTTP request which can be executed by the IDE.

It’s really simple…create a new file in your Solution/Project, for example endpoints-to-test.http and paste the following code:

### Do a GET on my homepage
GET https://ryansouthgate.com

Your IDE should look like this:

Screenshot of Visual Studio, showing a new Action button next to the line with the Url on

If you don’t see the icon above, next to your request, then you might need to update your Visual Studio 2022 installation to the latest avaiable version.

Now click the icon and Visual Studio will run the request, and show you the output. It’ll look like this:

Screenshot of Visual Studio, showing a new Action button next to the line with the Url on

You’re greeted with the response headers from my server and the html content.

You can even use variables and modify headers, and you’ll get intellisense for variable completion.

### (Example) Update a User on a locally-hosted service
@userId=5

PUT https://localhost:5072/users/{{userId}}
Authorization: Bearer Super-Secret-Bearer-Token
Content-Type: application/json
{
    "Id": {{userId}},
    "FirstName": "Ryan",
    "LastName"": "Southgate",
    "Age": 34,
    "Address": {
        "Line1": "99 The Road",
        "City": "London",
        "PostCode": "LDN 123"
    }    
}

Inspired by VS Code

This functionality has been inspired by a VS Code Exension called REST Client which debuted in 2020. REST Client is much more feature-rich (take a look at the huge list of features on that link) and is only designed for VS Code. But it is nice to see Visual Studio get a Microsoft Supported version of this.

There is a Visual Studio Extension developed by Mads Kristensen, however given he works on the Visual Studio team, and it was last updated in September 2022. I wouldn’t install it, and instead use the built-in functionality in Visual Studio. As that seems to be the place where future features/improvement work will happen.

Why bother with http files anyway?

There’s a couple of reasons why this feature is important to Visual Studio.

  1. In the same folder/location as your API/Server you can include a list of pre-made API call examples. This can help on-boarding new developers and also gives people a really quick way to shoot a request at an endpoint, check it’s response and even create test data.
  2. With the ability to run this quickly inside your IDE, it stops you context switching to other applications that perform a similar task. Which leads me nicely on to some alternatives…

Alternatives

It’s not accurate to call Postman an alternative, Postman has a million more features than .http files in Visual Studio. Postman is your one-stop-shop for developing/testing APIs. It’s great, if you’ve not got it you’re missing out on making your development flow/feedback loop quicker.

Among the plethora of features Postman offers, below are a few of my favourite, which tighten my workflow:

  • responses have syntax higlighting and tree-navigation
  • can manage cookies that are returned/set by a service
  • keeps a history of request/responses
  • collections/runners - Postman allows you to setup “Recipies” of service calls, where you can call different services in order, and can extract values from the output of one, in order to use in one or more subsequent requests
  • output these collections/runners into files, which can then be stored in source control, next to your source code

For a full-featured API, Postman can’t be beat. However, for running a simple request or two, without the need for some of the “bells and whistles” above, then the simplicity of Visual Studio will suffice for many people.

What I’d like to see for Visual Studio Http

I’m unaware of the backlog for building out this feature set within Visual Studio, but the list of features for the VS Code extension is probably a good starting point.

Some of the things I would like to see in future improvements of this would be:

  • integration with the VS Test Panel (these files are not Unit Tests, but being able to see all in a project and run easily, would be good)
  • maybe some integration with the “UnitTest” runner, could describe the calls in http files, and then run asserts in C#? or run the asserts in the .http files themselves
  • syntax highlighting for responses

Conclusion

There hasn’t been much fanfare about this functionality in Visual Studio, so I’m assuming this is just the start of things to come and might be regarded as a “silent-launch”. I’m hoping they continue to build out this functionality and am excited for the features to come.

There’s also currently (at time of writing) no comprehensive documentation about the features supported by Visual Studio Http files.

For now though, if you need to run a quick HTTP call, simply, without loading up Postman or moving from your IDE, then Visual Studio wins for me. However, anything more complex than that, I’d stick with Postman.

Thanks for reading and let me know what you think about this and what features you’d like to see in the future



comments powered by Disqus