mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Merge pull request #135 from collinjackson/request_body
Support for HTTP request body using DataPipeFiller
This commit is contained in:
commit
16d7a95a11
65
examples/widgets/http_post.dart
Normal file
65
examples/widgets/http_post.dart
Normal file
@ -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<PostDemo> {
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -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 = <mojo.MojoDataPipeConsumer>[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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user