Discussion:
[dart-misc] Send a file via POST (server-side only)
Rodrigo Muniz
2017-03-03 15:21:39 UTC
Permalink
Hi folks!

I am developing a Telegram Bot, and I need to send a file via POST as
attachment to an user (e.g. a .txt file). Upon this, I have two questions:


- Which file type should I use in order to send a file? I heard about
Stream, but I am unsure about it;
- Having the right file type, how can I send it as a parameter for the
url POST? I tried the http library and built-in solutions. Here is a
snippet of the code I'm using right now:

Future<dynamic> sendRequest(String method, url, {json}) async {
BaseClient bs = new IOClient();
var request = new Request(method, url);
request.headers['Content-Type'] = 'application/json';
request.body = JSON.encode(json);
var streamedResponse = await bs.send(request);
var response = await Response.fromStream(streamedResponse);

var bodyJson;
try {
bodyJson = JSON.decode(response.body);
} on FormatException {
var contentType = response.headers['content-type'];
if (contentType != null && !contentType.contains('application/json'))
{
throw new Exception(
'Returned value was not JSON. Did the uri end with ".json"?');
}
rethrow;
}

if (response.statusCode != 200) {
if (bodyJson is Map) {
var error = bodyJson['error'];
if (error != null) {
throw error;
}
}
throw bodyJson;
}

return bodyJson;
}

Any ideas?
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...