import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
http.Response response = await createUser("Bob", "engineer");
print(response.body);
}
Future<http.Response> createUser(String name, String job) {
return http.post(
Uri.parse('https://reqres.in/api/users'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'name': name,
'job': job
}),
);
}
import 'package:http/http.dart' as http;
var url = Uri.parse('https://example.com/whatsit/create');
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print(await http.read('https://example.com/foobar.txt'));
import 'dart:convert';
import 'package:http/http.dart' as http;
void main {
final response = await http.get(Uri.parse("https://domain.com/endpoint?data=hello"));
String responseData = utf8.decode(response.bodyBytes);
print(json.decode(responseData));
}
var client = http.Client();
try {
var uriResponse = await client.post(Uri.parse('https://example.com/whatsit/create'),
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
class UserAgentClient extends http.BaseClient {
final String userAgent;
final http.Client _inner;
UserAgentClient(this.userAgent, this._inner);
Future<http.StreamedResponse> send(http.BaseRequest request) {
request.headers['user-agent'] = userAgent;
return _inner.send(request);
}
}