simplehttpconnection 1.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new79

Simple HTTP Connection #

Simple HTTP Connection, a library that i made to help me request data from internet while i develop flutter app, so i think i wanted to share it here ☺ This library using Http, Made with love by Moch. Nizwar Syafuan

Let's Code! #

var url = "https://postman-echo.com/get"
ResponseHttp respHttp = await  HttpConnection.doConnection(url);
print("Status Code : ${respHttp.statusCode}");
print("Status Headers : ${respHttp.headers}");
print("Content : ${respHttp.content}");

You can convert it to JSON easily, just put asJson() after content and you are ready to go :)

...
//it will return null if content isn't valid to convert to json
print("Content : ${respHttp.content.asJson()}");
...

Now, you can also making multiple requests to the same server, just keep open a presisten connection like this

...
ResponseHttp respHttp = await httpConnection.connect(tryUrl, method: Method.post); 
httpConnection.close(); //Close, if you do not want to use it
...
httpConnection.initConnection();   ///You have to init the connection if you want to use it again
respHttp = await httpConnection.connect(tryUrl, method: Method.post); 
...
httpConnection.close();

Request Stuff #

Get #

Psst....i made GET request look fun!

...
///Get Request
Map<String, String> params = Map();
params["foo1"] = "bar1";
params["foo2"] = "bar2";
ResponseHttp respHttp =await  HttpConnection.doConnection(url, method: Method.get, body: params);
...

Post #

...
HttpConnection.doConnection(url, method: HttpConnection.post, body: params);
...

Put #

...
HttpConnection.doConnection(url, method: HttpConnection.put, body: params);
...

Patch #

...
HttpConnection.doConnection(url, method: HttpConnection.patch, body: params);
...

Delete #

...
HttpConnection.doConnection(url, method: HttpConnection.delete);
...

Encoding #

Default encoding is UTF8, you can change it of course

...
HttpConnection.doConnection(url, encoding: utf8);
...

Download #

Now you can download file from SimpleDownload in simpliest way :D Thanks to Eka Setiawan Saputra from Flutter Indonesia for inspiring me... Updated, now you can controll your download progress....

... 
SimpleDownload simpleDownload = SimpleDownload();
print(await simpleDownload.start(
    "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
    path: "D:", downloadProgress: (progress, count, max) {
    print("Downloading : " + progress.toString() + "%");
})); 
simpleDownload.pause();  //Pause your download
simpleDownload.resume(); //Resume your download 
simpleDownload.stop();   //Stop your download
...

[1.3.0] - 30 Sept 2019

  • Download file from SimpleDownload
  • Add head method

[1.2.1] - 26 Sept 2019

  • Add get filesize by url function.

[1.2.0] - 26 Sept 2019

  • Add download function.
  • Add get filename by url function.
  • Update doc.

[1.1.0] - 23 Sept 2019

  • Add class method.
  • You can making multiple requests to same server by keep open a presistent connection.
  • Error handler

[1.0.1] - 22 Sept 2019

  • Renew doc (i forgot about this).

[1.0.0] - 22 Sept 2019

  • First release.

example/main.dart

@Timeout(Duration(days: 1))
import 'package:flutter_test/flutter_test.dart';
import 'package:simplehttpconnection/simplehttpconnection.dart';

main() {
  String tryUrl = "https://postman-echo.com/get";
  HttpConnection httpConnection = HttpConnection();
  group('Test Stuff', () {
    test('httpConnection Operation', () async {
      ResponseHttp respHttp =
          await httpConnection.connect(tryUrl, method: Method.post);
      print("Status Code 1 : ${respHttp.statusCode}");
      print("Status Headers 1 : ${respHttp.headers.toString()}");
      print("Content 1 : ${respHttp.content.toString()}");
      httpConnection.close();

      httpConnection.initConnection();

      ///After close, you have to init the connection
      respHttp = await httpConnection.connect(tryUrl, method: Method.post);
      print("Status Code 2 : ${respHttp.statusCode}");
      print("Status Headers 2 : ${respHttp.headers.toString()}");
      print("Content 2 : ${respHttp.content.toString()}");
      httpConnection.close();
    });

    test('Get Response from tryUrl', () async {
      ResponseHttp respHttp = await HttpConnection.doConnection(tryUrl);
      print("Status Code : ${respHttp.statusCode}");
      print("Status Headers : ${respHttp.headers.toString()}");
      print("Content : ${respHttp.content.toString()}");
    });

    test('tryUrl Response as JSON', () async {
      ResponseHttp respHttp = await HttpConnection.doConnection(tryUrl);
      print("Status Code : ${respHttp.statusCode}");
      print("Status Headers : ${respHttp.headers.toString()}");
      print("Content : ${respHttp.content.asJson()}");
      expect(respHttp.content.asJson() == null, false);
    });

    test('GET Request', () async {
      Map<String, String> params = Map();
      params["foo1"] = "bar1";
      params["foo2"] = "bar2";

      ResponseHttp respHttp = await HttpConnection.doConnection(tryUrl,
          method: Method.get, body: params);
      print("Status Code : ${respHttp.statusCode}");
      print("Status Headers : ${respHttp.headers.toString()}");
      print("Content : ${respHttp.content.asJson()}");
    });

    test("Download Data", () async {
      SimpleDownload simpleDownload = SimpleDownload();
      print(await simpleDownload.start(
          "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
          path: "D:", downloadProgress: (progress, count, max) {
        print("Downloading : " + progress.toString() + "%");
      }));

      simpleDownload.pause(); //Pause your download
      simpleDownload.resume(); //Resume your download
      simpleDownload.stop(); //Stop your download
    });
  });
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  simplehttpconnection: ^1.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:simplehttpconnection/simplehttpconnection.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
58
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
http ^0.12.0+2 0.12.0+2
path ^1.6.4 1.6.4
path_provider ^1.3.0 1.4.0
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
flutter 0.0.0
http_parser 3.1.3
meta 1.1.7
pedantic 1.8.0+1
platform 2.2.1
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test