惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
小众软件
小众软件
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
Last Week in AI
Last Week in AI
H
Help Net Security
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
腾讯CDC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Tip: add default headers to Serverpod endpoint calls toda...
Silfalion · 2026-04-28 · via DEV Community

Silfalion

Serverpod does not currently expose global default headers for generated endpoint HTTP calls, but you can work around that today by overriding the generated client and reissuing the HTTP request yourself. This is useful for values like Accept-Language that you want to send on most requests without adding an argument to every endpoint.

Client workaround

import 'package:http/http.dart' as http;
import 'package:serverpod_client/serverpod_client.dart';
import 'src/protocol/client.dart';

class AppClient extends Client {
  AppClient(super.host, [super.securityContext]);

  final _http = http.Client();

  final Map<String, String> defaultHeaders = {};

  @override
  Future<T> callServerEndpoint<T>(
    String endpoint,
    String method,
    Map<String, dynamic> args, {
    bool authenticated = true,
  }) async {
    final auth = authenticated ? await authKeyProvider?.authHeaderValue : null;

    final response = await _http.post(
      Uri.parse('$host$endpoint'),
      headers: {
        'content-type': 'application/json; charset=utf-8',
        if (auth != null) 'authorization': auth,
        ...defaultHeaders,
      },
      body: SerializationManager.encode({
        ...args,
        'method': method,
      }),
    ).timeout(connectionTimeout);

    if (response.statusCode != 200) {
      throw ServerpodClientException(response.body, response.statusCode);
    }

    if (T == getType<void>()) return null as T;
    return serializationManager.decode<T>(response.body, T);
  }

  @override
  void close() {
    _http.close();
    super.close();
  }
}

Enter fullscreen mode Exit fullscreen mode

Global usage

final client = AppClient('http://localhost:8080/')
  ..defaultHeaders['accept-language'] = 'fr-FR';

Enter fullscreen mode Exit fullscreen mode

Grouped usage

If only one feature area should get the extra headers, create a second preconfigured client for that feature area instead of mutating one shared client back and forth:

class LocalizedGreetingApi {
  LocalizedGreetingApi(Client baseClient)
    : client = AppClient(baseClient.host)
        ..authKeyProvider = baseClient.authKeyProvider
        ..defaultHeaders['accept-language'] = 'fr-FR';

  final AppClient client;

  Future<String> hello(String name) async {
    return client.greeting.hello(name);
  }
}

Enter fullscreen mode Exit fullscreen mode

Server-side read

Future<String> hello(Session session, String name) async {
  final language = session.request?.headers['accept-language']?.firstOrNull;
  return 'hello $name ($language)';
}

Enter fullscreen mode Exit fullscreen mode

Caveats

  • This is for normal generated endpoint HTTP calls only.
  • This is not for method streams.
  • Do not use this to override authorization.
  • Do not use this to override content-type.
  • If you need arbitrary custom headers from browser clients, remember CORS can matter.
  • This is a workaround until the framework supports default client headers directly.