Search
 
SCRIPT & CODE EXAMPLE
 

DART

flutter print http response

class MHttpClient {
  final http.Client client;
  final SharedPreferences sharedPreferences;
  MHttpClient(this.client, this.sharedPreferences);

  Future<http.Response> get(
      {String path = "", Map<String, String> extraHeders}) async {
    printWrapped('get Path: $path');
    final response = await client.get(
      Uri.parse(getBaseURL() + Version + path),
      headers: getHeaders(extraHeaders: extraHeders),
    );
    printWrapped("get response : 
" + utf8.decode(response.bodyBytes));
    return response;
  }

  Future<http.Response> post(
      {String body = "",
      String path = "",
      Map<String, String> extraHeders}) async {
    printWrapped('sended body: 
');
    printWrapped(' ${json.decode(body)}');
    final response = await client.post(
      Uri.parse(getBaseURL() + Version + path),
      body: body,
      headers: getHeaders(extraHeaders: extraHeders),
    );
    printWrapped("post response : 
" + utf8.decode(response.bodyBytes));
    return response;
  }

  Future<http.Response> put({String body = "", String path = ""}) async {
    printWrapped('put body: 
 ${json.decode(body)}');
    final response = await client.put(
      Uri.parse(getBaseURL() + Version + path),
      body: body,
      headers: getHeaders(),
    );
    printWrapped(utf8.decode(response.bodyBytes));
    return response;
  }

  Future<http.Response> putImage({File image, String path = ""}) async {
    printWrapped('Image Path: $path');
    final response = await http.put(
      Uri.parse(path),
      headers: getImageHeaders(),
      body: image.readAsBytesSync(),
    );
    return response;
  }

  String getBaseURL() {
    if (Foundation.kDebugMode)
      return BaseURLSTAGING;
    else
      return BaseURL;
  }

  String getApiKey() {
    if (Foundation.kDebugMode)
      return ApiKeyStaging;
    else
      return ApiKey;
  }

  String getToken() {
    String cashedToken = sharedPreferences.getString(CACHED_TOKEN);
    if (cashedToken == null) cashedToken = "";
    return cashedToken;
  }

  Map<String, String> getHeaders({Map extraHeaders}) {
    Map<String, String> headers = {
      'Content-Type': 'application/json; charset=UTF-8',
      'x-api-key': getApiKey(),
      HttpHeaders.authorizationHeader: 'Bearer ' + getToken(),
    };
    if (extraHeaders == null || extraHeaders.isEmpty)
      return headers;
    else {
      headers.addAll(extraHeaders);
      return headers;
    }
  }

  Map<String, String> getImageHeaders() {
    return <String, String>{'Content-Type': 'image/png'};
  }

  void printWrapped(String text) {
    final pattern = RegExp('.{400}'); // 800 is the size of each chunk
    pattern.allMatches(text).forEach((match) => developer.log(match.group(0)));
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: flutter vibration 
Dart :: flutter column in listview not working 
Dart :: flutter ios status bar is dark 
Dart :: flutter get image file from assets 
Dart :: listtile flutter 
Dart :: how to use flaticon as icon in flutter 
Dart :: dart check runtime type 
Dart :: regex dart 
Dart :: flutter calander last date + 6 days 
Dart :: convert string to file flutter 
Dart :: onbackpressed in flutter 
Dart :: flutter check null 
Dart :: dart find in array 
Dart :: listtile shape flutter 
Dart :: flexible alert dialog flutter 
Dart :: dart remove from list 
Dart :: package:mp3 player/play pause button.dart 
Dart :: flutter splash on tap 
Dart :: dart get class name 
Dart :: search functionality dart 
Dart :: flutter appbar is still grey 
Dart :: how to mesure execution time of method in dart 
Dart :: flutter: unhandled element defs; Picture key: AssetBundlePictureKey 
Dart :: * In pubspec.yaml the flutter.plugin.{androidPackage,iosPrefix,pluginClass} keys are deprecated. Instead use the flutter.plugin.platforms key introduced in Flutter 1.10.0 
Dart :: dart class with 
Swift :: delay code execution swift 5 
Swift :: swift continue 
Swift :: remove child from firebase swift 
Swift :: swift http request 
Swift :: swift programming language wikipedia 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =