diff --git a/examples/widgets/http_post.dart b/examples/widgets/http_post.dart new file mode 100644 index 00000000000..bf5ade5d1b3 --- /dev/null +++ b/examples/widgets/http_post.dart @@ -0,0 +1,65 @@ +// Copyright 2015, the Flutter project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:flutter/http.dart' as http; +import 'package:flutter/material.dart'; + +void main() { + runApp( + new MaterialApp( + title: "HTTP POST Example", + routes: { + '/': (RouteArguments args) => const PostDemo() + } + ) + ); +} + +class PostDemo extends StatefulComponent { + const PostDemo(); + PostDemoState createState() => new PostDemoState(); +} + +class PostDemoState extends State { + + String _response = null; + + void initState() { + _refresh(); + super.initState(); + } + + void _refresh() { + setState(() { + _response = null; + }); + http.post("http://httpbin.org/post", body: "asdf=42").then((http.Response response) { + setState(() { + _response = response.body; + }); + }); + } + + Widget build(BuildContext context) { + return new Scaffold( + toolBar: new ToolBar( + center: new Text("HTTP POST example") + ), + body: new Material( + child: new Block( + [new Text( + "${_response ?? 'Loading...'}", + style: Typography.black.body1 + )] + ) + ), + floatingActionButton: new FloatingActionButton( + child: new Icon( + icon: 'navigation/refresh' + ), + onPressed: _refresh + ) + ); + } +} diff --git a/packages/flutter/lib/src/http/mojo_client.dart b/packages/flutter/lib/src/http/mojo_client.dart index 881cde9c08d..77f14b135e3 100644 --- a/packages/flutter/lib/src/http/mojo_client.dart +++ b/packages/flutter/lib/src/http/mojo_client.dart @@ -70,8 +70,13 @@ class MojoClient { mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound(); mojo.UrlRequest request = new mojo.UrlRequest() ..url = url.toString() - ..method = method - ..body = body; + ..method = method; + if (body != null) { + mojo.MojoDataPipe pipe = new mojo.MojoDataPipe(); + request.body = [pipe.consumer]; + ByteData data = new ByteData.view(UTF8.encode(body).buffer); + mojo.DataPipeFiller.fillHandle(pipe.producer, data); + } try { _networkService.ptr.createUrlLoader(loader); mojo.UrlResponse response = (await loader.ptr.start(request)).response;