Skip to content

feat: Let a resource and an endpoint set headers for their requests - #214

Merged
slatecipher merged 1 commit into
mainlinefrom
feat/resource-and-method-headers
Dec 4, 2026
Merged

slatecipher merged 1 commit into
mainlinefrom
feat/resource-and-method-headers

Conversation

@slatecipher

Copy link
Copy Markdown
Owner

What and why

A client class can set headers sent on every request (#204), but a group of endpoints, or one endpoint, couldn't: an Accept: text/csv for one export endpoint had to be a Header() parameter every caller passed. This adds headers at the resource and endpoint levels.

  • Resource and AsyncResource take a headers= class argument, and each verb decorator a headers= option, as in @method.get("/export", headers={"Accept": "text/csv"}). They're checked like a client class's headers= when the class or decorator is defined.
  • They're added to each request when it's built. The most specific wins, name by name: the client's headers, then the resource's, then the endpoint's, then a Header() argument passed on the call.
  • A resource subclass's headers are merged over its parent's. A resource mounted on another doesn't get its headers, as for prefixes and tags.
  • The header check moves into _headers.py, shared by clients, resources and endpoints.
  • Client and resource reprs show header names, not values.
  • The class arguments are named in the ClientX, AsyncClientX and Resource docstrings, which say they must be passed by name.
  • Documented in the guide (docs/resources.md with a new sample, configuration.md, requests.md), the reference, llms.txt, CLAUDE.md and the changelog (### Added).

Closes #209

Testing

  • make check passes: lint, type checks, and 394 tests with the coverage gate.
  • New tests in test_clients.py and test_query_and_headers.py cover the order headers win in, resource subclass merging, mounted resources not getting their parent's headers, and the definition-time checks.
  • test/test_docs.py runs the new docs_src/resources/headers.py sample.

🤖 Generated with Claude Code

- Add a headers= class argument to Resource and AsyncResource, and a
  headers= option to each verb decorator, checked like a client class's
  headers= when the class or decorator is defined.
- Add them to each request when it's built: the most specific wins, name
  by name: the client's headers, then the resource's, then the
  endpoint's, then a Header() argument passed on the call.
- Merge a resource subclass's headers over its parent's. A resource
  mounted on another doesn't get its headers, as for prefixes and tags.
- Move the header check into _headers.py, shared by clients, resources
  and endpoints.
- Show header names, not values, in client and resource reprs.
- Name the class arguments in the ClientX, AsyncClientX and Resource
  docstrings, and say they must be passed by name.
- Document resource and endpoint headers in the guide, the reference,
  llms.txt, CLAUDE.md and the changelog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@slatecipher
slatecipher force-pushed the feat/resource-and-method-headers branch from 996613f to 475a550 Compare December 4, 2026 16:44
@slatecipher
slatecipher merged commit 1c436cc into mainline Dec 4, 2026
8 checks passed
@slatecipher
slatecipher deleted the feat/resource-and-method-headers branch December 4, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let a resource and an endpoint set headers for their requests

1 participant