Merge pull request #396 from collinjackson/headers
Support for setting HTTP headers
This commit is contained in:
commit
b2cdc16812
@ -2,6 +2,8 @@
|
|||||||
// for details. All rights reserved. Use of this source code is governed by a
|
// 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.
|
// BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/http.dart' as http;
|
import 'package:flutter/http.dart' as http;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
@ -30,15 +32,18 @@ class PostDemoState extends State<PostDemo> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _refresh() {
|
Future _refresh() async {
|
||||||
setState(() {
|
setState(() {
|
||||||
_response = null;
|
_response = null;
|
||||||
});
|
});
|
||||||
http.post("http://httpbin.org/post", body: "asdf=42").then((http.Response response) {
|
http.Response response = await http.post(
|
||||||
|
"http://httpbin.org/post",
|
||||||
|
body: "asdf=42",
|
||||||
|
headers: { "foo": "bar" }
|
||||||
|
);
|
||||||
setState(() {
|
setState(() {
|
||||||
_response = response.body;
|
_response = response.body;
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -29,15 +29,16 @@ Future<Response> head(url) =>
|
|||||||
/// This automatically initializes a new [Client] and closes that client once
|
/// 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 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.
|
/// the same server, you should use a single [Client] for all of those requests.
|
||||||
Future<Response> get(url) =>
|
Future<Response> get(url, {Map<String, String> headers}) =>
|
||||||
_withClient((client) => client.get(url));
|
_withClient((client) => client.get(url, headers: headers));
|
||||||
|
|
||||||
/// Sends an HTTP POST request with the given headers and body to the given URL,
|
/// Sends an HTTP POST request with the given headers and body to the given URL,
|
||||||
/// which can be a [Uri] or a [String].
|
/// which can be a [Uri] or a [String].
|
||||||
///
|
///
|
||||||
/// [body] sets the body of the request.
|
/// [body] sets the body of the request.
|
||||||
Future<Response> post(url, {body}) =>
|
Future<Response> post(url, {Map<String, String> headers, body}) =>
|
||||||
_withClient((client) => client.post(url, body: 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,
|
/// Sends an HTTP PUT request with the given headers and body to the given URL,
|
||||||
/// which can be a [Uri] or a [String].
|
/// which can be a [Uri] or a [String].
|
||||||
@ -46,8 +47,9 @@ Future<Response> post(url, {body}) =>
|
|||||||
/// a [Map<String, String>]. If it's a String, it's encoded using [encoding] and
|
/// 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
|
/// used as the body of the request. The content-type of the request will
|
||||||
/// default to "text/plain".
|
/// default to "text/plain".
|
||||||
Future<Response> put(url, {String body}) =>
|
Future<Response> put(url, {Map<String, String> headers, body}) =>
|
||||||
_withClient((client) => client.put(url, body: body));
|
_withClient((client) => client.put(url,
|
||||||
|
headers: headers, body: body));
|
||||||
|
|
||||||
/// Sends an HTTP PATCH request with the given headers and body to the given
|
/// Sends an HTTP PATCH request with the given headers and body to the given
|
||||||
/// URL, which can be a [Uri] or a [String].
|
/// URL, which can be a [Uri] or a [String].
|
||||||
@ -68,8 +70,9 @@ Future<Response> put(url, {String body}) =>
|
|||||||
///
|
///
|
||||||
/// For more fine-grained control over the request, use [Request] or
|
/// For more fine-grained control over the request, use [Request] or
|
||||||
/// [StreamedRequest] instead.
|
/// [StreamedRequest] instead.
|
||||||
Future<Response> patch(url, { body }) =>
|
Future<Response> patch(url, {Map<String, String> headers, body}) =>
|
||||||
_withClient((client) => client.patch(url, body: body));
|
_withClient((client) => client.patch(url,
|
||||||
|
headers: headers, body: body));
|
||||||
|
|
||||||
/// Sends an HTTP DELETE request with the given headers to the given URL, which
|
/// Sends an HTTP DELETE request with the given headers to the given URL, which
|
||||||
/// can be a [Uri] or a [String].
|
/// can be a [Uri] or a [String].
|
||||||
@ -79,8 +82,8 @@ Future<Response> patch(url, { body }) =>
|
|||||||
/// the same server, you should use a single [Client] for all of those requests.
|
/// 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.
|
/// For more fine-grained control over the request, use [Request] instead.
|
||||||
Future<Response> delete(url) =>
|
Future<Response> delete(url, {Map<String, String> headers}) =>
|
||||||
_withClient((client) => client.delete(url));
|
_withClient((client) => client.delete(url, headers: headers));
|
||||||
|
|
||||||
/// Sends an HTTP GET request with the given headers to the given URL, which can
|
/// 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
|
/// be a [Uri] or a [String], and returns a Future that completes to the body of
|
||||||
@ -95,8 +98,8 @@ Future<Response> delete(url) =>
|
|||||||
///
|
///
|
||||||
/// For more fine-grained control over the request and response, use [Request]
|
/// For more fine-grained control over the request and response, use [Request]
|
||||||
/// instead.
|
/// instead.
|
||||||
Future<String> read(url) =>
|
Future<String> read(url, {Map<String, String> headers}) =>
|
||||||
_withClient((client) => client.read(url));
|
_withClient((client) => client.read(url, headers: headers));
|
||||||
|
|
||||||
/// Sends an HTTP GET request with the given headers to the given URL, which can
|
/// 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
|
/// be a [Uri] or a [String], and returns a Future that completes to the body of
|
||||||
@ -111,8 +114,8 @@ Future<String> read(url) =>
|
|||||||
///
|
///
|
||||||
/// For more fine-grained control over the request and response, use [Request]
|
/// For more fine-grained control over the request and response, use [Request]
|
||||||
/// instead.
|
/// instead.
|
||||||
Future<Uint8List> readBytes(url) =>
|
Future<Uint8List> readBytes(url, {Map<String, String> headers}) =>
|
||||||
_withClient((client) => client.readBytes(url));
|
_withClient((client) => client.readBytes(url, headers: headers));
|
||||||
|
|
||||||
Future _withClient(Future fn(MojoClient client)) {
|
Future _withClient(Future fn(MojoClient client)) {
|
||||||
var client = new MojoClient();
|
var client = new MojoClient();
|
||||||
|
@ -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/core.dart' as mojo;
|
||||||
import 'package:mojo/mojo/url_request.mojom.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/url_response.mojom.dart' as mojo;
|
||||||
|
import 'package:mojo/mojo/http_header.mojom.dart' as mojo;
|
||||||
|
|
||||||
import 'response.dart';
|
import 'response.dart';
|
||||||
|
|
||||||
@ -63,10 +64,17 @@ class MojoClient {
|
|||||||
|
|
||||||
Future<Response> _send(String method, url,
|
Future<Response> _send(String method, url,
|
||||||
Map<String, String> headers, [body, Encoding encoding]) async {
|
Map<String, String> headers, [body, Encoding encoding]) async {
|
||||||
|
|
||||||
mojo.UrlLoaderProxy loader = new mojo.UrlLoaderProxy.unbound();
|
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()
|
mojo.UrlRequest request = new mojo.UrlRequest()
|
||||||
..url = url.toString()
|
..url = url.toString()
|
||||||
|
..headers = mojoHeaders
|
||||||
..method = method;
|
..method = method;
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
mojo.MojoDataPipe pipe = new mojo.MojoDataPipe();
|
mojo.MojoDataPipe pipe = new mojo.MojoDataPipe();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user