diff --git a/examples/widgets/http_post.dart b/examples/widgets/http_post.dart index bf5ade5d1b3..3664836ce95 100644 --- a/examples/widgets/http_post.dart +++ b/examples/widgets/http_post.dart @@ -2,6 +2,8 @@ // 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 'dart:async'; + import 'package:flutter/http.dart' as http; import 'package:flutter/material.dart'; @@ -30,14 +32,17 @@ class PostDemoState extends State { super.initState(); } - void _refresh() { + Future _refresh() async { setState(() { _response = null; }); - http.post("http://httpbin.org/post", body: "asdf=42").then((http.Response response) { - setState(() { - _response = response.body; - }); + http.Response response = await http.post( + "http://httpbin.org/post", + body: "asdf=42", + headers: { "foo": "bar" } + ); + setState(() { + _response = response.body; }); } diff --git a/packages/flutter/lib/src/http/http.dart b/packages/flutter/lib/src/http/http.dart index e70a70451ce..dbf811a9b71 100644 --- a/packages/flutter/lib/src/http/http.dart +++ b/packages/flutter/lib/src/http/http.dart @@ -29,15 +29,16 @@ Future head(url) => /// This automatically initializes a new [Client] and closes that client once /// the request is complete. If you're planning on making multiple requests to /// the same server, you should use a single [Client] for all of those requests. -Future get(url) => - _withClient((client) => client.get(url)); +Future get(url, {Map headers}) => + _withClient((client) => client.get(url, headers: headers)); /// Sends an HTTP POST request with the given headers and body to the given URL, /// which can be a [Uri] or a [String]. /// /// [body] sets the body of the request. -Future post(url, {body}) => - _withClient((client) => client.post(url, body: body)); +Future post(url, {Map headers, body}) => + _withClient((client) => client.post(url, + headers: headers, body: body)); /// Sends an HTTP PUT request with the given headers and body to the given URL, /// which can be a [Uri] or a [String]. @@ -46,8 +47,9 @@ Future post(url, {body}) => /// a [Map]. If it's a String, it's encoded using [encoding] and /// used as the body of the request. The content-type of the request will /// default to "text/plain". -Future put(url, {String body}) => - _withClient((client) => client.put(url, body: body)); +Future put(url, {Map headers, body}) => + _withClient((client) => client.put(url, + headers: headers, body: body)); /// Sends an HTTP PATCH request with the given headers and body to the given /// URL, which can be a [Uri] or a [String]. @@ -68,8 +70,9 @@ Future put(url, {String body}) => /// /// For more fine-grained control over the request, use [Request] or /// [StreamedRequest] instead. -Future patch(url, { body }) => - _withClient((client) => client.patch(url, body: body)); +Future patch(url, {Map headers, body}) => + _withClient((client) => client.patch(url, + headers: headers, body: body)); /// Sends an HTTP DELETE request with the given headers to the given URL, which /// can be a [Uri] or a [String]. @@ -79,8 +82,8 @@ Future patch(url, { body }) => /// the same server, you should use a single [Client] for all of those requests. /// /// For more fine-grained control over the request, use [Request] instead. -Future delete(url) => - _withClient((client) => client.delete(url)); +Future delete(url, {Map headers}) => + _withClient((client) => client.delete(url, headers: headers)); /// Sends an HTTP GET request with the given headers to the given URL, which can /// be a [Uri] or a [String], and returns a Future that completes to the body of @@ -95,8 +98,8 @@ Future delete(url) => /// /// For more fine-grained control over the request and response, use [Request] /// instead. -Future read(url) => - _withClient((client) => client.read(url)); +Future read(url, {Map headers}) => + _withClient((client) => client.read(url, headers: headers)); /// Sends an HTTP GET request with the given headers to the given URL, which can /// be a [Uri] or a [String], and returns a Future that completes to the body of @@ -111,8 +114,8 @@ Future read(url) => /// /// For more fine-grained control over the request and response, use [Request] /// instead. -Future readBytes(url) => - _withClient((client) => client.readBytes(url)); +Future readBytes(url, {Map headers}) => + _withClient((client) => client.readBytes(url, headers: headers)); Future _withClient(Future fn(MojoClient client)) { var client = new MojoClient(); diff --git a/packages/flutter/lib/src/http/mojo_client.dart b/packages/flutter/lib/src/http/mojo_client.dart index 8f0612ab224..0fb079a8454 100644 --- a/packages/flutter/lib/src/http/mojo_client.dart +++ b/packages/flutter/lib/src/http/mojo_client.dart @@ -12,6 +12,7 @@ import 'package:mojo_services/mojo/url_loader.mojom.dart' as mojo; import 'package:mojo/core.dart' as mojo; import 'package:mojo/mojo/url_request.mojom.dart' as mojo; import 'package:mojo/mojo/url_response.mojom.dart' as mojo; +import 'package:mojo/mojo/http_header.mojom.dart' as mojo; import 'response.dart'; @@ -63,10 +64,17 @@ class MojoClient { Future _send(String method, url, Map headers, [body, Encoding encoding]) async { - mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound(); + List mojoHeaders = []; + headers.forEach((String name, String value) { + mojo.HttpHeader header = new mojo.HttpHeader() + ..name = name + ..value = value; + mojoHeaders.add(header); + }); mojo.UrlRequest request = new mojo.UrlRequest() ..url = url.toString() + ..headers = mojoHeaders ..method = method; if (body != null) { mojo.MojoDataPipe pipe = new mojo.MojoDataPipe();