Search…
Fetch
The fetch command allows you to send and receive data to or from a server.
Fetch supports http and https but where possible you should always use https.
In its simplest form fetch is often used to download a file from a web server however you can also post data to a web server.
For complete API documentation refer to: pub.dev
DCli allows you to fetch a single web resource with progress information or to simultaneously fetch multiple resources.

Fetch a single resource

The resource 'sample.aac' will be downloaded and saved to the temporary file 'sample.aac'.
1
withTempFile((sampleAac) {
2
try {
3
String baseURl =
4
'https://raw.githubusercontent.com/noojee/dcli/master/test/src/functions/fetch_downloads';
5
fetch(url: '$baseURl/sample.aac', saveToPath: sampleAac);
6
} on FetchException catch (e) {
7
print('Exception Thrown: ${e.errorCode} ${e.message}');
8
}
9
/// print the returned data including any errors.
10
if (exists(tmp)) {
11
print(read(tmp).toParagraph());
12
}
13
}, create: false
14
, suffix: 'acc');
Copied!

Fetch as single resource and show progress

1
withTempFile((sampleAac) {
2
try {
3
fetch(url: '$baseURl/sample.aac',
4
saveToPath: sampleAac,
5
onProgress: (progress) {
6
print(progress);
7
});
8
} on FetchException catch (e) {
9
print('Exception Thrown: ${e.errorCode} ${e.message}');
10
}
11
/// print the returned data including any errors.
12
if (exists(tmp)) {
13
print(read(tmp).toParagraph());
14
}
15
}, create: false
16
, suffix: 'acc');
Copied!

Fetch multiple resource and show progress

1
void get() {
2
var sampleAac = fs.tempFile();
3
var sampleWav = fs.tempFile();
4
5
fetchMultiple(urls: [
6
FetchUrl(url: '$baseURl/sample.aac', saveToPath: sampleAac, onProgress: showProgress),
7
FetchUrl(url: '$baseURl/sample.wav', saveToPath: sampleWav)
8
]);
9
}
10
11
void showProgress(FetchProgress progress) {
12
print(progress);
13
}
Copied!

Post data to a server

Send the data contained in the 'content' variable to httpbin.org.
1
withTempFile((file) {
2
try
3
{
4
const content = 'Hellow World';
5
fetch(
6
url: 'https://httpbin.org/post',
7
method: FetchMethod.post,
8
data: FetchData.fromString(content),
9
saveToPath: file);
10
/// process the json response.
11
final map =
12
Parser(read(file).toList()).jsonDecode() as Map<String, dynamic>;
13
expect(map['data'] as String, equals(content));
14
expect(
15
(map['headers'] as Map<String, dynamic>)['Content-Type'] as String,
16
equals('text/plain'));
17
} on FetchException catch (e) {
18
print('Exception Thrown: ${e.errorCode} ${e.message}');
19
}
20
/// print the returned data including any errors.
21
if (exists(file)) {
22
print(read(file).toParagraph());
23
}
24
}, create: false);
25
Copied!

FetchData

When posting data to a server you can provide the data from a number of different sources using the appropriate FetchData constructor.

FetchData.fromString

Provides data to the fetch method contained in a String.
By default the mimeType is set to text/plain but you can override this by explicitly passing a mimeType
1
FetchData.fromString('Hello World', mimeType: 'plain.csv');
Copied!

FetchData.fromFile

The fromFile constructor uses a file as the source of the data to be posted.
By default fromFile will use the filename's extension to determine the mime type.
You can override the default behaviour by passing the mimeType to the constructor
1
FetchData.fromFile('mountains.png');
2
3
FetchData.fromFile('mountains', mimeType: 'image/png');
Copied!

FetchData.fromBytes

The fromBytes constructor allows you to set the source of data as a byte array.
1
withTempFile((pathToData) {
2
withTempFile((file) {
3
const bytes = <int>[0, 1, 2, 3, 4, 5];
4
5
fetch(
6
url: 'https://httpbin.org/post',
7
method: FetchMethod.post,
8
data: FetchData.fromBytes(bytes),
9
saveToPath: file);
10
11
}, create: false);
12
});
Copied!

FetchData.fromStream

The fromStream constructor allows you use a stream as the source of the post data.
By default the mimeType is set to 'application/octet-stream' but you can override this by passing an explicit mimeType to FetchData.fromStream.
1
withTempFile((pathToData) {
2
withTempFile((file) {
3
const content = 'Hellow World2';
4
pathToData.write(content);
5
6
fetch(
7
url: 'https://httpbin.org/post',
8
method: FetchMethod.post,
9
data: FetchData.fromStream(File(pathToData).openRead()),
10
saveToPath: file);
11
12
}, create: false);
13
});
Copied!

Custom Headers

The fetch function allows you to set custom HTTP headers.
The 'Content-Type' header will be overridden by the mimeType in FetchData.
1
withTempFile((file) {
2
fetch(url: 'https://httpbin.org/get',
3
headers: {'X-Test-Header1': 'Value1', 'X-Test-Header2': 'Value2'},
4
saveToPath: file);
5
6
}, create: false);
Copied!
Last modified 1mo ago