Right, so I've been working on something which requires basic authentication through headers, and passing some variables via HTTP Post. This is a terminal app.
This is what my code looks like:
import 'package:http/http.dart' as http;
import 'dart:io';
void main() {
var url = "http://httpbin.org/post";
var client = new http.Client();
var request = new http.Request('POST', Uri.parse(url));
var body = {'content':'this is a test', 'email':'john@doe.com', 'number':'441276300056'};
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json; charset=utf-8';
request.headers[HttpHeaders.AUTHORIZATION] = 'Basic 021215421fbe4b0d27f:e74b71bbce';
request.body = body;
var future = client.send(request).then((response) => response.stream.bytesToString().then((value) => print(value.toString()))).catchError((error) => print(error.toString()));
}
I'm using httpbin as an echo server, so it tells me what I'm passing in. My code works correctly if I don't pass a body, or if I pass a string as the body.
Obviously that's because the body attribute in http.Request only accepts strings, and I'm trying to pass a map to it.
I could convert that to a string, and it would probably work, but I still think my code could be improved. Not from a syntax point of view, or from how it's handling the future, but I'm not certain using http.dart is the right thing to do.
Could someone point me in the right direction?
Thanks in advance.