Skip to content

Using .http Files for API Testing in .NET 8 Projects

Problem Statement

When creating a new ASP.NET Core project in .NET 8, developers often notice an Api.http file in their solution but are unclear about its purpose or functionality. This unfamiliar file raises questions:

  • What role does it serve in API development?
  • How does it interact with Visual Studio?
  • Can it replace traditional API testing tools?

This confusion stems from recent additions to the .NET project templates that include this file without immediate context.

Solution Overview

The Api.http file is Visual Studio 2022's integrated solution for testing ASP.NET Core APIs. This declarative format allows you to define and execute HTTP requests in lightweight text files, directly within your development environment.

Key Advantages

  • Zero-Dependency Testing - Eliminates need for Postman/curl during development
  • Version Control Friendly - Plain text format integrates with Git
  • Direct Project Integration - Maintains request definitions alongside API code
  • Real-time Debugging - View responses in Visual Studio's editor

Getting Started with .http Files

File Structure Basics

.http files follow a simple syntax:

http
### Request name (optional)
GET http://localhost:5034/api/products

### Create new product
POST http://localhost:5034/api/products
Content-Type: application/json

{
  "name": "Wireless Mouse",
  "price": 29.99
}

Executing Requests

  1. Open the Api.http file
  2. Click the green Send Request link above any request
  3. View response directly in the editor

Visual Studio showing Send Request link above HTTP verb

Core Functionality

Variable Support

Define reusable variables for environments:

http
@hostname = localhost
@port = 5034
@host = {{hostname}}:{{port}}
@contentType = application/json

### Get all products
GET http://{{host}}/api/products

Authorization

Handle authentication tokens automatically:

http
@token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

### Create protected resource
POST http://localhost:5034/api/secure-endpoint
Authorization: Bearer {{token}}

Response Handling

  • Syntax highlighting for JSON/XML responses
  • Response history accessible via View > Other Windows > Web API Client
  • Time measurements for performance testing

Advanced Patterns

File Uploads

http
POST http://localhost:5034/api/upload
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="document.pdf"

\< ./path/to/document.pdf
--WebAppBoundary--

Chained Requests

Extract data from responses for subsequent calls:

http
# @name auth
POST http://localhost:5034/auth/login
Content-Type: {{contentType}}

{
  "username": "admin",
  "password": "password"
}

###
@token = {{auth.response.headers.X-AuthToken}}

GET http://localhost:5034/api/protected
Authorization: Bearer {{token}}

Comparison to Alternatives

Feature.http FilesPostmancurl
Editor Integration✔️ Native
Version Control✔️ Text✔️*✔️
No Setup Required✔️✔️
Complex Scripting✔️
Cloud Sync✔️

* Postman supports syncing collections but requires account

Best Practices

Workflow Suggestions

  1. Include sample requests in project template
  2. Create environment-specific variables (dev/prod)
  3. Organize requests with ### section dividers
  4. Store sensitive tokens in .env files

Limitations to Consider

  • Lacks UI for response visualization
  • No automated testing capabilities
  • Limited to HTTP(S) protocols only

To enable full environment management:

  1. Create http-client.env.json
json
{
  "dev": {
    "host": "localhost:5034"
  },
  "prod": {
    "host": "api.example.com"
  }
}
  1. Reference variables:
http
GET https://{{host}}/api/status

This integrated approach reduces friction in development workflows, allowing immediate testing without context switching between applications.

Official Resources