Skip to content

Commit

Permalink
Fix flutter#104 Support for setting HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson committed Nov 16, 2015
1 parent d5e71f7 commit e3b9094
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
15 changes: 10 additions & 5 deletions examples/widgets/http_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -30,14 +32,17 @@ class PostDemoState extends State<PostDemo> {
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;
});
}

Expand Down
31 changes: 17 additions & 14 deletions packages/flutter/lib/src/http/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ Future<Response> 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<Response> get(url) =>
_withClient((client) => client.get(url));
Future<Response> get(url, {Map<String, String> 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<Response> post(url, {body}) =>
_withClient((client) => client.post(url, body: body));
Future<Response> post(url, {Map<String, String> 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].
Expand All @@ -46,8 +47,9 @@ Future<Response> post(url, {body}) =>
/// a [Map<String, String>]. 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<Response> put(url, {String body}) =>
_withClient((client) => client.put(url, body: body));
Future<Response> put(url, {Map<String, String> 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].
Expand All @@ -68,8 +70,9 @@ Future<Response> put(url, {String body}) =>
///
/// For more fine-grained control over the request, use [Request] or
/// [StreamedRequest] instead.
Future<Response> patch(url, { body }) =>
_withClient((client) => client.patch(url, body: body));
Future<Response> patch(url, {Map<String, String> 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].
Expand All @@ -79,8 +82,8 @@ Future<Response> 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<Response> delete(url) =>
_withClient((client) => client.delete(url));
Future<Response> delete(url, {Map<String, String> 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
Expand All @@ -95,8 +98,8 @@ Future<Response> delete(url) =>
///
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<String> read(url) =>
_withClient((client) => client.read(url));
Future<String> read(url, {Map<String, String> 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
Expand All @@ -111,8 +114,8 @@ Future<String> read(url) =>
///
/// For more fine-grained control over the request and response, use [Request]
/// instead.
Future<Uint8List> readBytes(url) =>
_withClient((client) => client.readBytes(url));
Future<Uint8List> readBytes(url, {Map<String, String> headers}) =>
_withClient((client) => client.readBytes(url, headers: headers));

Future _withClient(Future fn(MojoClient client)) {
var client = new MojoClient();
Expand Down
10 changes: 9 additions & 1 deletion packages/flutter/lib/src/http/mojo_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -63,10 +64,17 @@ class MojoClient {

Future<Response> _send(String method, url,
Map<String, String> headers, [body, Encoding encoding]) async {

mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound();
List<mojo.HttpHeader> mojoHeaders = <mojo.HttpHeader>[];
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();
Expand Down

0 comments on commit e3b9094

Please sign in to comment.