title: Doing an HTTP Post with headers and a body
body: <p>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.</p>
  
  <p>This is what my code looks like:</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>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) =&gt; response.stream.bytesToString().then((value) =&gt; print(value.toString()))).catchError((error) =&gt; print(error.toString()));
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Obviously that's because the body attribute in http.Request only accepts strings, and I'm trying to pass a map to it.</p>
  
  <p>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.</p>
  
  <p>Could someone point me in the right direction?</p>
  
  <p>Thanks in advance.</p>