x402 icon indicating copy to clipboard operation
x402 copied to clipboard

fix: preserve Headers object during 402 retry in x402-fetch

Open dankelleher opened this issue 5 months ago • 4 comments

Summary

  • Fixed Headers object preservation during 402 retry in x402-fetch
  • Replaced spread operator with Headers constructor to properly handle all HeadersInit types
  • Added test coverage for Headers object preservation

Problem

The retry logic in wrapFetchWithPayment used the spread operator on init.headers, which doesn't work correctly with Headers objects. The spread operator does not iterate through the actual header entries, causing headers to be lost during retry.

Example of the issue:

// Original headers
const headers = new Headers({
  'Accept': 'application/json, text/event-stream',
  'Content-Type': 'application/json'
});

// Spread operator fails - results in empty objects
{ ...headers } 

Solution

Use the Headers constructor which properly handles all HeadersInit types:

  • Headers objects: new Headers(headersObj) clones all entries
  • Plain objects: new Headers({ 'Accept': 'application/json' }) works as expected
  • Arrays: new Headers([['Accept', 'application/json']]) converts correctly

Impact

This fix ensures headers are preserved during retry.

dankelleher avatar Aug 04 '25 07:08 dankelleher

✅ Heimdall Review Status

Requirement Status More Info
Reviews 1/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

cb-heimdall avatar Aug 04 '25 07:08 cb-heimdall

@dankelleher is attempting to deploy a commit to the Coinbase Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Aug 04 '25 07:08 vercel[bot]

@dankelleher you need to sign your commits, otherwise lgtm

apmcdermott-cb avatar Aug 15 '25 20:08 apmcdermott-cb

@dankelleher ping-- need you to sign your commits but otherwise g2g

apmcdermott-cb avatar Sep 17 '25 19:09 apmcdermott-cb