[DART-dio] Can't send body and binary data
Created by: jaumard
I have the following definition:
"/api/v1/shops/{shop_id}/transactions/{id}/signature":
patch:
operationId: patch_shops_transaction_signature_partial
description: ""
requestBody:
content:
image/png:
schema:
type: string
nullable: true
format: binary
description: signature image to upload
required: true
responses:
"201":
description: All good
tags:
- transactions
This is generated like this:
Future<Response<void>> patchShopsTransactionSignaturePartial(
int id,
int shopId,
Uint8List body, {
CancelToken cancelToken,
Map<String, dynamic> headers,
Map<String, dynamic> extra,
ValidateStatus validateStatus,
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
}) async {
final _request = RequestOptions(
path: r'/api/v1/shops/{shop_id}/transactions/{id}/signature'.replaceAll('{' r'id' '}', id.toString()).replaceAll('{' r'shop_id' '}', shopId.toString()),
method: 'PATCH',
headers: <String, dynamic>{
...?headers,
},
extra: <String, dynamic>{
'secure': <Map<String, String>>[
{
'type': 'apiKey',
'name': 'Bearer',
'keyName': 'Authorization',
'where': 'header',
},
],
...?extra,
},
validateStatus: validateStatus,
contentType: [
'image/png',
].first,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
dynamic _bodyData;
_bodyData = body;
final _response = await _dio.request<dynamic>(
_request.path,
data: _bodyData,
options: _request,
);
return _response;
}
But this is not working, the correct way of sending the data is like this according to this issue of dio (https://github.com/flutterchina/dio/issues/1036):
Future<Response<void>> patchShopsTransactionSignaturePartial(
int id,
int shopId,
Uint8List body, {
CancelToken cancelToken,
Map<String, dynamic> headers,
Map<String, dynamic> extra,
ValidateStatus validateStatus,
ProgressCallback onSendProgress,
ProgressCallback onReceiveProgress,
}) async {
final _request = RequestOptions(
path: r'/api/v1/shops/{shop_id}/transactions/{id}/signature'.replaceAll('{' r'id' '}', id.toString()).replaceAll('{' r'shop_id' '}', shopId.toString()),
method: 'PATCH',
headers: <String, dynamic>{
...?headers,
'Content-length': body.length,
},
extra: <String, dynamic>{
'secure': <Map<String, String>>[
{
'type': 'apiKey',
'name': 'Bearer',
'keyName': 'Authorization',
'where': 'header',
},
],
...?extra,
},
validateStatus: validateStatus,
contentType: [
'application/octet-stream',
].first,
cancelToken: cancelToken,
onSendProgress: onSendProgress,
onReceiveProgress: onReceiveProgress,
);
List<int> _bodyData;
_bodyData = body;
final _response = await _dio.request<dynamic>(
_request.path,
data: Stream.fromIterable(_bodyData.map((e) => [e])),
options: _request,
);
return _response;
}