Skip to content

Commit 4140791

Browse files
authored
Merge pull request #3 from Virtual-Protocol/feat/acp-837
disable endpoint response compression so x402scan can display correctly
2 parents 8a0fee3 + 886f6e4 commit 4140791

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

examples/python/servers/fastapi/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ async def acp_budget(request: Request) -> Dict[str, Any]:
161161
Handle both GET and POST requests for ACP budget payment.
162162
x402scan may use either method depending on the request.
163163
"""
164-
return {
164+
from fastapi.responses import JSONResponse
165+
166+
response_data = {
165167
"message": "pay acp job budget",
166168
"token": "acp job payment token",
167169
"protocol": "x402",
@@ -170,6 +172,15 @@ async def acp_budget(request: Request) -> Dict[str, Any]:
170172
"advice": "not financial advice",
171173
"method": request.method # Show which method was used
172174
}
175+
176+
# Explicitly disable compression for x402scan compatibility
177+
return JSONResponse(
178+
content=response_data,
179+
headers={
180+
"Content-Encoding": "identity", # Tell CloudFlare: no compression
181+
"Cache-Control": "no-transform", # Prevent any transformation
182+
}
183+
)
173184

174185

175186
# @app.get("/premium/content")

python/x402/src/x402/fastapi/middleware.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def x402_response(error: str):
146146
accepts=payment_requirements,
147147
error=error,
148148
).model_dump(by_alias=True)
149-
headers = {"Content-Type": "application/json"}
149+
headers = {
150+
"Content-Type": "application/json",
151+
"Content-Encoding": "identity", # Disable compression for x402scan
152+
"Cache-Control": "no-transform", # Prevent any transformation
153+
}
150154

151155
return JSONResponse(
152156
content=response_data,
@@ -206,6 +210,9 @@ def x402_response(error: str):
206210
response.headers["X-PAYMENT-RESPONSE"] = base64.b64encode(
207211
settle_response.model_dump_json(by_alias=True).encode("utf-8")
208212
).decode("utf-8")
213+
# Disable compression for x402scan compatibility
214+
response.headers["Content-Encoding"] = "identity"
215+
response.headers["Cache-Control"] = "no-transform"
209216
else:
210217
return x402_response(
211218
"Settle failed: "

0 commit comments

Comments
 (0)