POST JSON with a bearer token
A typical API call with headers, a query parameter and a JSON body, converted for Dio.
cURL command
curl -X POST 'https://api.example.com/v1/orders?notify=true' \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-d '{"customer_id": 1024, "items": [{"sku": "ZV-1001", "qty": 2}], "gift": false}'
Dart code
import 'package:dio/dio.dart';
Future<Response<dynamic>> sendRequest() async {
final Dio dio = Dio();
final Response<dynamic> response = await dio.request<dynamic>(
'https://api.example.com/v1/orders',
queryParameters: <String, dynamic>{
'notify': 'true',
},
data: <String, dynamic>{
'customer_id': 1024,
'items': <dynamic>[
<String, dynamic>{
'sku': 'ZV-1001',
'qty': 2,
},
],
'gift': false,
},
options: Options(
method: 'POST',
headers: <String, dynamic>{
'Authorization': 'Bearer YOUR_TOKEN',
},
contentType: 'application/json',
),
);
return response;
}
- Add Dio to pubspec.yaml: dio: ^5.0.0