我如何在 flutter 中使用以下 api 和 dio 包?

how can i use the following api with dio package in flutter?

这是请求购物车详细信息的模型,但我如何在 flutter 中使用 dio 包将其存档 -H 和 -u 代表什么???

    curl -X POST https://example.com/wp-json/cocart/v2/cart/add-item \
    -u username:password \
    -H "Content-Type: application/json" \
    -d '{
      "id": "32",
      "quantity": "1"
       }'

首先,您需要了解 api calls

的最新动态

回答您的问题,因为这是一个 post 请求


import 'package:dio/dio.dart';
import 'dart:convert';
void sendData() async {
String username = 'test';
  String password = '123£';
//lets encode creds
  String basicAuth =
      'Basic ' + base64Encode(utf8.encode('$username:$password'));
  print(basicAuth);

  try {
    var response = await Dio().post('https://example.com/wp-json/cocart/v2/cart/add-item',
 options:Options(headers: <String, String>{'authorization': basicAuth}),
 data:{
      "id": "32",
      "quantity": "1"
       }
 );
    print(response);
  } catch (e) {
    print(e);
  }
}