I want to upload a file via a non-multipart/form-data
request like this:
POST http://127.0.0.1/upload
Cache-Control: no-cache
< /path/to/file/in/disk
(I tested and it successfully uploaded a file from JetBrain Rider's REST client to my REST endpoint.)
There is a StreamedRequest
class in http
package but I didn't find any constructor or setter to plug a byte stream or file content into it.
How do I use StreamedRequest
to upload a file in Dart?
In some older code of mine I use
/// Send a POST request to the Docker service.
Future<http.ByteStream> _streamRequestStream(
String path, Stream<List<int>> stream,
{Map<String, String> query}) async {
assert(stream != null);
final url = serverReference.buildUri(path, query);
final request = new http.StreamedRequest('POST', url)
..headers.addAll(headersTar);
stream.listen(request.sink.add);
final http.BaseResponse response =
await request.send().then(http.Response.fromStream);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw new DockerRemoteApiError(
response.statusCode, response.reasonPhrase, null);
}
return (response as http.StreamedResponse).stream;
}
which might do what you want
https://github.com/bwu-dart/bwu_docker/blob/master/lib/src/remote_api.dart#L160-L178
It uses the http
package
import 'package:http/http.dart' as http;