Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
There was a problem hiding this comment.
Greptile Summary
This PR addresses a critical "double body reading" issue that occurs when HTTP request bodies are consumed multiple times in Node.js/Next.js environments, which causes stream consumption errors since request bodies can only be read once.
The fix spans four key files with coordinated changes:
apps/sim/app/api/proxy/route.ts: The core fix replaces direct request.json() calls with a two-step process: first reading the body as text using request.text(), then parsing with JSON.parse(). This prevents the request stream from being consumed multiple times and provides better error logging with the raw request text for debugging.
apps/sim/tools/index.ts: Updates routing documentation to clarify that external APIs should always use the POST proxy endpoint rather than GET querystring variants, ensuring consistent body handling patterns.
apps/sim/tools/utils.ts: Enhances the formatRequestParams function to properly validate body content by checking for undefined values rather than just function existence, and adds support for function-based HTTP method definitions. This prevents attempting to stringify undefined bodies.
apps/sim/tools/http/request.ts: Significantly refactors proxy request handling by simplifying URL construction (removing complex query string encoding) and using response.clone().json() for safe response inspection without consuming the original body.
These changes work together to ensure that HTTP request/response bodies are only read once throughout the entire request lifecycle, from initial proxy handling through tool execution and response processing. The architectural shift moves all proxy requests through the POST /api/proxy endpoint for consistent handling.
PR Description Notes:
- The PR description is incomplete with placeholder text
- No issue number is referenced despite the "Fixes #(issue)" template
- Testing section and type of change checkboxes are not filled out
4 files reviewed, 1 comment
There was a problem hiding this comment.
Greptile Summary
This PR systematically fixes double body reading issues across the HTTP request handling pipeline in the SimStudio API. The core problem was that HTTP Response bodies can only be read once, but the codebase was attempting multiple reads in various scenarios, causing runtime failures.
The fix implements a comprehensive solution across four key files:
-
HTTP Request Tool (
apps/sim/tools/http/request.ts): The main fix usesresponse.clone().json()to safely inspect response content without consuming the original body. The proxy logic is simplified to return original URLs instead of encoding parameters into proxy URLs, shifting to consistent use of the POST/api/proxyendpoint. -
Tool Index (
apps/sim/tools/index.ts): Implements the nullish coalescing operator (??) to handle cases where cloned response parsing succeeds but returns falsy values, preventing fallback attempts to read the original response body. -
Proxy API Route (
apps/sim/app/api/proxy/route.ts): Convertsrequest.json()to a saferrequest.text()+JSON.parse()pattern, preserving the raw request body for potential multiple accesses and improving error logging. -
Utils (
apps/sim/tools/utils.ts): Fixes method parameter handling to support functions (like other request properties), ensures proper method resolution precedence, and corrects thehasBodylogic to check for actual content rather than just function existence.
These changes work together to create a robust HTTP request handling system that avoids stream consumption conflicts while maintaining functionality for both direct API calls and proxied requests. The architectural shift toward consistent POST proxy usage also eliminates query string length limitations and encoding issues.
PR Description Notes:
- The PR template is incomplete - summary, issue reference, change type, and testing details are missing
- All checkboxes remain unchecked
Confidence score: 4/5
- This PR addresses a critical runtime issue with a well-architected solution that prevents double body reading errors
- Score reflects comprehensive fixes across the request pipeline, though the changes touch multiple core files simultaneously
- Pay close attention to the response cloning logic in
apps/sim/tools/http/request.tsand the method resolution changes inapps/sim/tools/utils.ts
4 files reviewed, no comments
* fix(double-read-http): double reading body json * fix * fix tests
Summary
API Block Fixes
Type of Change
Testing
Manually with GET and POST
Checklist