dotnet-httpie
Search Results for

    Show / Hide Table of Contents

    File Execution

    πŸ“– ζŸ₯ηœ‹δΈ­ζ–‡ζ–‡ζ‘£

    dotnet-httpie can execute HTTP requests from .http and .rest files, making it perfect for API testing, documentation, and automation.

    Overview

    The exec command allows you to run HTTP requests defined in files, supporting:

    • Standard .http and .rest file formats
    • Variable substitution
    • Environment-specific configurations
    • Request chaining and referencing

    Basic Usage

    Execute Single File

    dotnet-http exec requests.http
    

    Execute with Environment

    dotnet-http exec requests.http --env production
    

    Execute Specific Request Type

    dotnet-http exec requests.http --type http
    dotnet-http exec curl-commands.curl --type curl
    

    HTTP File Format

    Basic Request

    # Get user information
    GET https://api.example.com/users/123
    Authorization: Bearer your-token
    

    Multiple Requests

    # Get all users
    GET https://api.example.com/users
    Authorization: Bearer your-token
    
    ###
    
    # Create new user
    POST https://api.example.com/users
    Content-Type: application/json
    Authorization: Bearer your-token
    
    {
      "name": "John Doe",
      "email": "john@example.com"
    }
    
    ###
    
    # Update user
    PUT https://api.example.com/users/123
    Content-Type: application/json
    Authorization: Bearer your-token
    
    {
      "name": "John Smith",
      "email": "john.smith@example.com"
    }
    

    Request with Variables

    @baseUrl = https://api.example.com
    @token = your-bearer-token
    
    # Get user
    GET {{baseUrl}}/users/123
    Authorization: Bearer {{token}}
    
    ###
    
    # Create user with dynamic data
    POST {{baseUrl}}/users
    Content-Type: application/json
    Authorization: Bearer {{token}}
    
    {
      "name": "User {{$randomInt}}",
      "email": "user{{$randomInt}}@example.com",
      "timestamp": "{{$datetime iso8601}}"
    }
    

    Named Requests

    @baseUrl = https://api.example.com
    
    ###
    
    # @name getUser
    GET {{baseUrl}}/users/123
    
    ###
    
    # @name createUser
    POST {{baseUrl}}/users
    Content-Type: application/json
    
    {
      "name": "John Doe",
      "email": "john@example.com"
    }
    

    Environment Files

    HTTP Client Environment File

    Create http-client.env.json:

    {
      "development": {
        "baseUrl": "http://localhost:3000",
        "apiKey": "dev-api-key"
      },
      "staging": {
        "baseUrl": "https://staging-api.example.com",
        "apiKey": "staging-api-key"
      },
      "production": {
        "baseUrl": "https://api.example.com",
        "apiKey": "prod-api-key"
      }
    }
    

    Using Environment Variables

    # This will use variables from the specified environment
    GET {{baseUrl}}/users
    X-API-Key: {{apiKey}}
    

    Execute with specific environment:

    dotnet-http exec api-requests.http --env production
    

    Variable Types

    Built-in Variables

    # Random values
    POST {{baseUrl}}/users
    Content-Type: application/json
    
    {
      "id": "{{$uuid}}",
      "name": "User {{$randomInt}}",
      "email": "user{{$randomInt}}@example.com",
      "timestamp": "{{$datetime iso8601}}",
      "created": "{{$timestamp}}"
    }
    

    Environment Variables

    # Access system environment variables
    GET {{baseUrl}}/secure
    Authorization: Bearer {{$env API_TOKEN}}
    

    Custom Variables

    @userId = 123
    @apiVersion = v2
    
    GET {{baseUrl}}/{{apiVersion}}/users/{{userId}}
    

    Request Referencing

    Reference responses from previous requests:

    # @name login
    POST {{baseUrl}}/auth/login
    Content-Type: application/json
    
    {
      "username": "admin",
      "password": "password"
    }
    
    ###
    
    # Use token from login response
    GET {{baseUrl}}/protected/data
    Authorization: Bearer {{login.response.body.token}}
    
    ###
    
    # Reference request headers
    GET {{baseUrl}}/audit
    X-Original-Request-Id: {{login.request.headers.X-Request-ID}}
    

    Curl File Execution

    dotnet-httpie can also execute curl commands from files:

    Curl File Format

    # file: api-calls.curl
    
    # Get user data
    curl -X GET "https://api.example.com/users/123" \
      -H "Authorization: Bearer token" \
      -H "Accept: application/json"
    
    # Create new user
    curl -X POST "https://api.example.com/users" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer token" \
      -d '{
        "name": "John Doe",
        "email": "john@example.com"
      }'
    

    Execute Curl File

    dotnet-http exec api-calls.curl --type curl
    

    Advanced Features

    Request Chaining

    # @name createUser
    POST {{baseUrl}}/users
    Content-Type: application/json
    
    {
      "name": "John Doe",
      "email": "john@example.com"
    }
    
    ###
    
    # @name getUserProfile
    # @depends createUser
    GET {{baseUrl}}/users/{{createUser.response.body.id}}/profile
    
    ###
    
    # @name updateProfile
    # @depends getUserProfile
    PUT {{baseUrl}}/users/{{createUser.response.body.id}}/profile
    Content-Type: application/json
    
    {
      "bio": "Updated bio",
      "avatar": "{{getUserProfile.response.body.avatar}}"
    }
    

    Debugging File Execution

    Debug Mode

    dotnet-http exec requests.http --debug
    

    Offline Mode (Preview)

    dotnet-http exec requests.http --offline
    

    This shows what requests would be sent without actually executing them.

    Verbose Output

    dotnet-http exec requests.http --verbose
    

    Organization Strategies

    Project Structure

    project/
    β”œβ”€β”€ api-tests/
    β”‚   β”œβ”€β”€ auth/
    β”‚   β”‚   β”œβ”€β”€ login.http
    β”‚   β”‚   └── logout.http
    β”‚   β”œβ”€β”€ users/
    β”‚   β”‚   β”œβ”€β”€ create-user.http
    β”‚   β”‚   β”œβ”€β”€ get-user.http
    β”‚   β”‚   └── update-user.http
    β”‚   └── http-client.env.json
    β”œβ”€β”€ environments/
    β”‚   β”œβ”€β”€ development.env.json
    β”‚   β”œβ”€β”€ staging.env.json
    β”‚   └── production.env.json
    └── scripts/
        β”œβ”€β”€ setup.http
        β”œβ”€β”€ cleanup.http
        └── health-check.http
    

    File Naming Conventions

    • Use descriptive names: create-user.http, get-order-details.http
    • Group by feature: auth/, users/, orders/
    • Use environment prefixes: dev-setup.http, prod-health.http

    CI/CD Integration

    GitHub Actions Example

    name: API Tests
    on: [push, pull_request]
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v6
          - name: Setup .NET
            uses: actions/setup-dotnet@v5
            with:
              dotnet-version: '10.0.x'
          - name: Install dotnet-httpie
            run: dotnet tool install --global dotnet-httpie
          - name: Run API tests
            run: dotnet-http exec tests/api-tests.http --env testing
    

    Azure DevOps Example

    steps:
    - task: DotNetCoreCLI@2
      displayName: 'Install dotnet-httpie'
      inputs:
        command: 'custom'
        custom: 'tool'
        arguments: 'install --global dotnet-httpie'
    
    - script: dotnet-http exec api-tests/health-check.http --env $(Environment)
      displayName: 'Run Health Check'
    

    Best Practices

    1. Use environment files for different deployment environments
    2. Name your requests for better organization and referencing
    3. Group related requests in the same file
    4. Use variables instead of hardcoding values
    5. Add comments to explain complex requests
    6. Validate responses where critical
    7. Test offline first to verify request structure
    8. Version control your HTTP files alongside your code

    Examples

    Complete API Test Suite

    @baseUrl = https://api.example.com
    @contentType = application/json
    
    ###
    
    # @name healthCheck
    GET {{baseUrl}}/health
    
    ###
    
    # @name authenticate
    POST {{baseUrl}}/auth/login
    Content-Type: {{contentType}}
    
    {
      "username": "testuser",
      "password": "testpass"
    }
    
    ###
    
    # @name createUser
    POST {{baseUrl}}/users
    Authorization: Bearer {{authenticate.response.body.token}}
    Content-Type: {{contentType}}
    
    {
      "name": "Test User",
      "email": "test@example.com"
    }
    
    ###
    
    # @name getUser
    GET {{baseUrl}}/users/{{createUser.response.body.id}}
    Authorization: Bearer {{authenticate.response.body.token}}
    
    ###
    
    # @name updateUser
    PUT {{baseUrl}}/users/{{createUser.response.body.id}}
    Authorization: Bearer {{authenticate.response.body.token}}
    Content-Type: {{contentType}}
    
    {
      "name": "Updated Test User"
    }
    
    ###
    
    # @name deleteUser
    DELETE {{baseUrl}}/users/{{createUser.response.body.id}}
    Authorization: Bearer {{authenticate.response.body.token}}
    

    Next Steps

    • Set up CI/CD integration with HTTP files
    • Check common use cases for more examples
    • Edit this page
    In this article
    Back to top Generated by DocFX