Skip to content

Instantly share code, notes, and snippets.

@surfmuggle
Last active September 22, 2023 15:47
Show Gist options
  • Save surfmuggle/3d7c6619214dfe2a681648e9dd739be0 to your computer and use it in GitHub Desktop.
Save surfmuggle/3d7c6619214dfe2a681648e9dd739be0 to your computer and use it in GitHub Desktop.
keen-nebula-4294
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
void main(List<String> arguments) async {
final urlHttpBin = Uri.https('httpbin.org', '/basic-auth/myuser/mypasswd');
const base64Encoder = convert.Base64Encoder();
var creds64 = base64Encoder.convert('myuser:mypasswd'.codeUnits);
// https://pub.dev/documentation/http/latest/http/Client-class.html
final client = http.Client();
final request = http.Request('GET', urlHttpBin);
request.headers['Authorization'] = 'Basic $creds64';
// https://pub.dev/documentation/http/latest/http/StreamedResponse-class.html
final streamedResponse = await client.send(request);
// https://pub.dev/documentation/http/latest/http/Response/fromStream.html
final response = await http.Response.fromStream(streamedResponse);
// https://pub.dev/documentation/http/latest/http/Response-class.html
print(response.body);
client.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment