Skip to content

feat: supports custom client-side header configuration for API calls#113

Merged
chyroc merged 2 commits intocoze-dev:mainfrom
Littlezjh:fix_req_header
Mar 3, 2026
Merged

feat: supports custom client-side header configuration for API calls#113
chyroc merged 2 commits intocoze-dev:mainfrom
Littlezjh:fix_req_header

Conversation

@Littlezjh
Copy link
Contributor

@Littlezjh Littlezjh commented Mar 2, 2026

When creating a custom CozeAPI client using the following code snippet, the custom headers fail to be included in the outgoing requests. This PR adds support for passing the custom headers (e.g., X-Use-Ppe, X-Tt-Env) to the actual HTTP requests sent by the CozeAPI client:
client = coze.NewCozeAPI(
coze.NewTokenAuth(cozeConf.CozeAPIToken),
coze.WithBaseURL(cozeBaseUrl),
coze.WithHttpClient(&http.Client{Timeout: 600 * time.Second}),
coze.WithHeaders(
http.Header{
"X-Use-Ppe": []string{"1"},
"X-Tt-Env": []string{os.Getenv("SERVICE_ENV")},
}))

Summary by CodeRabbit

  • Bug Fixes
    • Client-level default headers are now merged into outgoing requests so global defaults are included alongside request-specific headers; request headers can still override defaults.

@CLAassistant
Copy link

CLAassistant commented Mar 2, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e516d94 and 29f5d45.

📒 Files selected for processing (1)
  • request.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • request.go

📝 Walkthrough

Walkthrough

Adds client-level default header merging to parseHeader in request.go: it iterates ins.headers and assigns the first value of each header into r.Headers before processing req.Headers, augmenting the request header set with global/client defaults.

Changes

Cohort / File(s) Summary
Header merging in parser
request.go
Inserted logic in parseHeader to iterate ins.headers and assign each header's first value into r.Headers prior to handling req.Headers, thereby introducing client/global default headers into the request.*

* The merge runs before request-specific headers are processed, so those can override these defaults.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I tiptoe through headers, one by one,
Plucking the first like a morning sun,
I plant them in requests, a gentle start,
Then watch request headers play their part,
A hop, a merge — unity before the run!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: supports custom client-side header configuration for API calls' accurately reflects the main change: adding support for custom headers in the CozeAPI client to be included in outgoing HTTP requests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@request.go`:
- Around line 246-251: The current merge into r.Headers uses v[0] which can
panic if a header key has an empty slice (ins.headers[k] == nil or len==0), and
the merge order causes client-level headers to override per-request headers; fix
by first populating r.Headers with client defaults, then overlaying ins.headers
so request-level wins, and when iterating ins.headers (in the block using
ins.headers and r.Headers) check len(v) > 0 before reading v[0] (skip entries
with empty slices or handle them explicitly) to avoid panics; update the merge
logic that touches ins.headers and r.Headers accordingly.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c701843 and e516d94.

📒 Files selected for processing (1)
  • request.go

@chyroc chyroc added the feature label Mar 3, 2026
@chyroc chyroc merged commit 01875f3 into coze-dev:main Mar 3, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants