tentacle_response_formatter 0.1.0

  • README.md
  • Installing
  • Versions
  • 30

Tentacle Response Formatter

A small class that, given a shelf request and some simple data, will determine a correct response format (eg.: Json or XML) and convert the data to the appropriate format.

How to use

// ResponseFormatter is a Singleton so you will always get the same instance with new
ResponseFormatter formatter = new ResponseFormatter();

// create a result from a shelf.Request and data
FormatResult result = formatter.formatResponse(request, {"message": "hello"});

//body contains the generated response as String
print(result.body) // -> '{"message":"hello"}' or '<response><message>hello</message></response>' or 'hello'

//contentType is a suggestion to set as the HttpHeaders.CONTENT_TYPE in the shelf.Response you create
print(result.contentType) // -> "application/json" or "application/xml" or "text/plain"

How is the response format detected

There are currently 3 values that influence the response format detection. They are applyed in the following order:

  1. Format query param: "http://example.com/some?format=json" would force the response to be converted to Json.
  2. Format file extension: "http://example.com/some.xml" would force the response to be converted to XML.
  3. Accept Header: "accept: application/json, text/xml" would force the response to be converted to Json.

Accept headers are used in the order they are defined. So first format has precedence over second and so forth.

If no format can be detected that has a valid encoder registered the response is encoded as String. If data contains a message field it is taken for String response otherwise the result will be an empty String. The string formatter is not a real formatter but rather a fallback. Read later on how to add your own formatter and register it for content types.

Add your own response formatter

You may want to add your own formatters or content-type mappings to the formatter. To do so you have to provide a formatter function which takes dynamic data and returns a String. Furthermore you have to provide a unique name and may provide your custom content-types that should be handled by your formatter.

// add a simple formatter for csv
formatter.registerFormatter("csv", "text/csv", (dynamic data) {
  return "Your CSV result here";
// now ?format=csv, file.csv and accept header "text/csv" are formatted using your formatter

// override additional accept-headers
formatter.registerFormatter("allmighty", "text/allmighty", (dynamic data) {
      return "allmighty response body";
}, ["application/json", "application/xhtml+xml", "*/*", "text/plain", "application/xml"]);

// replace existing formatter
formatter.registerFormatter("json", "application/json", (dynamic data) {
  return "Your json result here";
// json is now handled by your formatter including all accept-headers json was initially registered for


Apache 2.0

Use this package as a library

1. Depend on it

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

  tentacle_response_formatter: "^0.1.0"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

      import 'package:tentacle_response_formatter/formatter.dart';

      import 'package:tentacle_response_formatter/xml_converter.dart';
Version Uploaded Documentation Archive
0.1.0 Apr 16, 2014 Go to the documentation of tentacle_response_formatter 0.1.0 Download tentacle_response_formatter 0.1.0 archive


We analyzed this package on May 22, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.54.0
  • pana: 0.11.1


Describes how popular the package is relative to other packages. [more]
1 / 100
Code health derived from static analysis. [more]
97 / 100
Reflects how tidy and up-to-date the package is. [more]
0 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: Flutter, web, other

No platform restriction found in libraries.


  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use tentacle_response_formatter.dart.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 4 hints.

    Run dartfmt to format lib/formatter.dart.

    Run dartfmt to format lib/xml_converter.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.3.0
shelf >=0.5.0+1 <0.6.0 0.5.7 0.7.3+1
xml >=1.0.0 <2.0.0 1.0.0 3.0.0
Transitive dependencies
analyzer 0.25.0+1 0.32.0
args 0.13.7 1.4.3
async 1.13.3 2.0.7
barback 0.15.2+16
browser 0.10.0+3
charcode 1.1.1
collection 1.14.9
convert 1.1.1 2.0.1
crypto 0.9.2+1 2.0.3
glob 1.1.5
http 0.11.3+16
http_multi_server 1.3.2 2.0.4
http_parser 0.0.2+8 3.1.2
matcher 0.12.0+2 0.12.2+1
mime 0.9.6+1
package_config 1.0.3
package_resolver 1.0.2
path 1.5.1
pool 1.3.5
pub_semver 1.4.1
shelf_static 0.2.7 0.2.7+1
shelf_web_socket 0.0.1+5 0.2.2+1
source_map_stack_trace 1.1.4
source_maps 0.10.5
source_span 1.4.0
stack_trace 1.9.2
string_scanner 0.1.5 1.0.2
test 0.12.3+9 0.12.38+1
typed_data 1.1.5
watcher 0.9.7+7
yaml 2.1.14
Dev dependencies
unittest any 0.12.3 0.12.4+1