update
This commit is contained in:
43
readme.md
43
readme.md
@@ -237,6 +237,49 @@ The response object provides these methods:
|
||||
|
||||
Each body method can only be called once per response, similar to the fetch API.
|
||||
|
||||
### Important: Always Consume Response Bodies
|
||||
|
||||
**You should always consume response bodies, even if you don't need the data.** Unconsumed response bodies can cause:
|
||||
- Memory leaks as data accumulates in buffers
|
||||
- Socket hanging with keep-alive connections
|
||||
- Connection pool exhaustion
|
||||
|
||||
```typescript
|
||||
// ❌ BAD - Response body is not consumed
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/status')
|
||||
.get();
|
||||
|
||||
if (response.ok) {
|
||||
console.log('Success!');
|
||||
}
|
||||
// Socket may hang here!
|
||||
|
||||
// ✅ GOOD - Response body is consumed
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/status')
|
||||
.get();
|
||||
|
||||
if (response.ok) {
|
||||
console.log('Success!');
|
||||
}
|
||||
await response.text(); // Consume the body even if not needed
|
||||
```
|
||||
|
||||
In Node.js, SmartRequest automatically drains unconsumed responses to prevent socket hanging, but it's still best practice to explicitly consume response bodies. When auto-drain occurs, you'll see a console log: `Auto-draining unconsumed response body for [URL] (status: [STATUS])`.
|
||||
|
||||
You can disable auto-drain if needed:
|
||||
```typescript
|
||||
// Disable auto-drain (not recommended unless you have specific requirements)
|
||||
const response = await SmartRequest.create()
|
||||
.url('https://api.example.com/data')
|
||||
.autoDrain(false) // Disable auto-drain
|
||||
.get();
|
||||
|
||||
// Now you MUST consume the body or the socket will hang
|
||||
await response.text();
|
||||
```
|
||||
|
||||
## Advanced Features
|
||||
|
||||
### Form Data with File Uploads
|
||||
|
Reference in New Issue
Block a user