mirror of
https://github.com/foss42/apidash.git
synced 2025-06-01 14:58:28 +08:00
Adapt request body viewer for different MimeTypes
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import 'kvrow_model.dart';
|
||||
import '../consts.dart';
|
||||
|
||||
@ -102,7 +104,9 @@ class ResponseModel {
|
||||
this.headers,
|
||||
this.requestHeaders,
|
||||
this.contentType,
|
||||
this.mediaType,
|
||||
this.body,
|
||||
this.bodyBytes,
|
||||
this.time,
|
||||
});
|
||||
|
||||
@ -110,7 +114,9 @@ class ResponseModel {
|
||||
final Map<String, String>? headers;
|
||||
final Map<String, String>? requestHeaders;
|
||||
final String? contentType;
|
||||
final MediaType? mediaType;
|
||||
final String? body;
|
||||
final Uint8List? bodyBytes;
|
||||
final Duration? time;
|
||||
|
||||
ResponseModel fromResponse({
|
||||
@ -118,6 +124,12 @@ class ResponseModel {
|
||||
Duration? time,
|
||||
}) {
|
||||
var contentType = response.headers[HttpHeaders.contentTypeHeader];
|
||||
MediaType? mediaType;
|
||||
try {
|
||||
mediaType = MediaType.parse(contentType!);
|
||||
} catch (e) {
|
||||
mediaType = null;
|
||||
}
|
||||
final responseHeaders = mergeMaps(
|
||||
{HttpHeaders.contentLengthHeader: response.contentLength.toString()},
|
||||
response.headers);
|
||||
@ -126,9 +138,11 @@ class ResponseModel {
|
||||
headers: responseHeaders,
|
||||
requestHeaders: response.request?.headers,
|
||||
contentType: contentType,
|
||||
body: contentType == kJsonMimeType
|
||||
mediaType: mediaType,
|
||||
body: (mediaType?.subtype == kSubTypeJson)
|
||||
? utf8.decode(response.bodyBytes)
|
||||
: response.body,
|
||||
bodyBytes: response.bodyBytes,
|
||||
time: time,
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user