postJson<T> method
Future<ClientResult>
postJson<T>({
- required String url,
- required Map<
String, dynamic> body, - required T fromJson(),
- required OnSuccessTyped<
T> onSuccess, - required OnError onError,
- Map<
String, String> ? headers, - Duration? timeout,
- ClientType? clientType,
- String? cancelKey,
- OnHttpError? onHttpError,
- Map<
int, OnStatus> ? onStatus, - Set<
int> ? successCodes, - Set<
int> ? errorCodes,
Performs a POST request with typed JSON response.
Automatically parses the response body as JSON and deserializes it
using the provided fromJson function.
Implementation
Future<ClientResult> postJson<T>({
required String url,
required Map<String, dynamic> body,
required T Function(Map<String, dynamic> json) fromJson,
required OnSuccessTyped<T> onSuccess,
required OnError onError,
Map<String, String>? headers,
Duration? timeout,
ClientType? clientType,
String? cancelKey,
OnHttpError? onHttpError,
Map<int, OnStatus>? onStatus,
Set<int>? successCodes,
Set<int>? errorCodes,
}) async {
return post(
url: url,
body: body,
headers: headers,
timeout: timeout,
clientType: clientType,
cancelKey: cancelKey,
onSuccess: (response) {
try {
final json = response.jsonBody;
if (json == null) {
onError(ClientException(
message: 'Invalid JSON response',
url: url,
type: ClientErrorType.badResponse,
responseBody: response.body,
));
return;
}
final data = fromJson(json);
onSuccess(data, response);
} catch (e) {
onError(ClientException(
message: 'Failed to parse JSON: $e',
url: url,
type: ClientErrorType.unknown,
originalError: e,
));
}
},
onError: onError,
onHttpError: onHttpError,
onStatus: onStatus,
successCodes: successCodes,
errorCodes: errorCodes,
);
}