Skip to content

Commit 56706fc

Browse files
Copilotfarfromrefug
andcommitted
Add usage examples demonstrating iOS/Android parity behavior
Agent-Logs-Url: https://github.com/nativescript-community/https/sessions/8051dbe0-1e71-4045-88d9-5e25d50ae83e Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>
1 parent 844a0c1 commit 56706fc

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

docs/USAGE_EXAMPLE.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# iOS/Android Behavior Example
2+
3+
## Current Behavior (Consistent Across Platforms)
4+
5+
```typescript
6+
import { request } from '@nativescript-community/https';
7+
8+
async function downloadFile() {
9+
console.log('Starting download...');
10+
11+
// Step 1: Make the request
12+
// Both iOS and Android load the response data into memory
13+
const response = await request({
14+
method: 'GET',
15+
url: 'https://example.com/data.zip',
16+
onProgress: (current, total) => {
17+
const percent = (current / total * 100).toFixed(1);
18+
console.log(`Downloading: ${percent}%`);
19+
}
20+
});
21+
22+
// Step 2: Request completes, inspect the response
23+
console.log('Download complete!');
24+
console.log('Status code:', response.statusCode);
25+
console.log('Content-Type:', response.headers['Content-Type']);
26+
console.log('Content-Length:', response.contentLength);
27+
28+
// Step 3: Now decide what to do with the data
29+
if (response.statusCode === 200) {
30+
// Option A: Save to file
31+
const file = await response.content.toFile('~/Downloads/data.zip');
32+
console.log('Saved to:', file.path);
33+
34+
// Option B: Get as ArrayBuffer (alternative)
35+
// const buffer = await response.content.toArrayBuffer();
36+
// console.log('Buffer size:', buffer.byteLength);
37+
38+
// Option C: Parse as JSON (if applicable)
39+
// const json = response.content.toJSON();
40+
// console.log('Data:', json);
41+
} else {
42+
console.error('Download failed with status:', response.statusCode);
43+
}
44+
}
45+
46+
// Example with error handling
47+
async function downloadWithErrorHandling() {
48+
try {
49+
const response = await request({
50+
method: 'GET',
51+
url: 'https://example.com/large-file.pdf',
52+
timeout: 60, // 60 seconds
53+
onProgress: (current, total) => {
54+
console.log(`Progress: ${current}/${total}`);
55+
}
56+
});
57+
58+
// Check status first
59+
if (response.statusCode >= 400) {
60+
throw new Error(`HTTP ${response.statusCode}`);
61+
}
62+
63+
// Verify content type
64+
const contentType = response.headers['Content-Type'] || '';
65+
if (!contentType.includes('pdf')) {
66+
console.warn('Warning: Expected PDF but got:', contentType);
67+
}
68+
69+
// Save to file
70+
const file = await response.content.toFile('~/Documents/file.pdf');
71+
console.log('Successfully saved:', file.path);
72+
73+
return file;
74+
75+
} catch (error) {
76+
console.error('Download failed:', error.message);
77+
throw error;
78+
}
79+
}
80+
81+
// Example with conditional processing
82+
async function downloadAndProcess() {
83+
const response = await request({
84+
method: 'GET',
85+
url: 'https://api.example.com/data'
86+
});
87+
88+
console.log('Received response:', response.statusCode);
89+
90+
// Decide what to do based on content type
91+
const contentType = response.headers['Content-Type'] || '';
92+
93+
if (contentType.includes('json')) {
94+
// Parse as JSON
95+
const json = response.content.toJSON();
96+
console.log('JSON data:', json);
97+
return json;
98+
99+
} else if (contentType.includes('image')) {
100+
// Save as image file
101+
const file = await response.content.toFile('~/Pictures/image.jpg');
102+
console.log('Image saved:', file.path);
103+
104+
// iOS: Can also convert to ImageSource
105+
// const image = await response.content.toImage();
106+
107+
return file;
108+
109+
} else {
110+
// Save as generic file
111+
const file = await response.content.toFile('~/Downloads/data.bin');
112+
console.log('File saved:', file.path);
113+
return file;
114+
}
115+
}
116+
```
117+
118+
## Key Points
119+
120+
1. **Request completes with data in memory** (both platforms)
121+
2. **Inspect response first** (status, headers, content length)
122+
3. **Then decide how to process** (toFile, toArrayBuffer, toJSON, etc.)
123+
4. **Same behavior on iOS and Android** (cross-platform consistency)
124+
125+
## Platform Implementation
126+
127+
### iOS (Alamofire)
128+
- Response data is NSData in memory
129+
- `toFile()` writes NSData to disk: `data.writeToFileAtomically(path, true)`
130+
- `toArrayBuffer()` converts NSData to ArrayBuffer
131+
- `toJSON()` deserializes NSData as JSON
132+
133+
### Android (OkHttp)
134+
- Response data is in ResponseBody
135+
- `toFile()` streams ResponseBody to disk via InputStream
136+
- `toArrayBuffer()` reads ResponseBody into ByteBuffer
137+
- `toJSON()` parses ResponseBody as JSON
138+
139+
## Memory Considerations
140+
141+
Both platforms load response data for processing:
142+
- **Small files (<10MB)**: No issues
143+
- **Medium files (10-50MB)**: Should work on most devices
144+
- **Large files (>50MB)**: Monitor memory usage, test on target devices
145+
146+
This is the expected behavior for both platforms and matches standard HTTP client behavior (fetch API, Axios, etc.).

0 commit comments

Comments
 (0)