inspect network traffic in Flutter without running an app

Viewed 365

I made a few network requests in a Dart file that I execute separately using as a playground and not connected to my main app.

Is there any way to capture and analyze network activity made by it without actually executing that script form my main app?

So far I see the implementation of this as binding the execution of ьн sandbox script to a button tap in the app. But perhaps this can be done more simply.

1 Answers

1 - You can use software like fiddler to analyze the traffic on your local machine

2 - You can test your call in vscode using thunder client or postman to test request outside the IDE

3 - you can build a simple dart package to test request in dart code ouside of the actual code of the app (via flutter create package):

Code:

library shfl;

import 'dart:convert';
import 'package:http/http.dart' as http;

class Stuff {
static Future<String> getLiveHtml(String url) async {
  var resp = await http.get(
    Uri.parse(url),
    headers: {
      'User-Agent':
          'Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'
    },
  );
  return resp.body;
}
}

Test:

import 'package:flutter_test/flutter_test.dart';

import 'package:shfl/shfl.dart';

void main() {
  test('prints html from google', () async {
    var rez = await Stuff.getLiveHtml("https://google.com/");
    print(rez);
  });
}

Related