Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Ajax

Ajax: Send Custom Headers with Requests

- 24.06.26 - ErcanOPAK

📋 Headers = Request Metadata

API needs authentication, content type, custom data. Request headers send additional info. Essential for API integration.

📝 Sending Headers

// Fetch with headers
const response = await fetch('/api/data', {
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token',
    'X-API-Key': 'your-api-key',
    'Accept': 'application/json'
  }
});

// With authentication
const token = 'jwt-token-here';
await fetch('/api/protected', {
  headers: {
    'Authorization': `Bearer ${token}`
  }
});

// Multiple custom headers
await fetch('/api/custom', {
  headers: {
    'X-Request-ID': crypto.randomUUID(),
    'X-Client-Version': '1.0.0',
    'X-Environment': 'production'
  }
});

// Using Headers constructor
const headers = new Headers();
headers.append('Authorization', `Bearer ${token}`);
headers.append('Content-Type', 'application/json');
const response = await fetch('/api/data', { headers });

🎯 Common Headers

// Authentication
Authorization: Bearer 
X-API-Key: 

// Content
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
Accept: application/json

// Caching
Cache-Control: no-cache
If-Modified-Since: Mon, 15 Jan 2024 12:00:00 GMT

// Client info
User-Agent: MyApp/1.0.0
Accept-Language: en-US

// Custom
X-Request-ID: unique-id
X-Client-Version: 1.0.0
X-Environment: production

💡 Best Practices

  • Set Content-Type for POST/PUT requests
  • Send Authorization token for protected APIs
  • Use X-Request-ID for tracing
  • Don’t send sensitive data in headers
  • Check API documentation for required headers

“API requires auth header. Added Authorization: Bearer token. Request works. Headers are essential for secure APIs.”

— API Integration Developer

Related posts:

Ajax Requests Succeed but UI Never Updates

Stop Using JSON for Everything — Sometimes HTML Is Faster

Ajax: Use FormData to Send Form Data Without Page Reload

Post Views: 3

Post navigation

JavaScript: Use setTimeout and setInterval for Timers
Git: Use Git Add to Stage Changes for Commit

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com