Home > Back-end >  how can i use the following api with dio package in flutter?
how can i use the following api with dio package in flutter?

Time:02-24

this is the model of requesting cart details but how can I archive this using dio package in flutter and what does that -H and -u stands for???

    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"
       }'

CodePudding user response:

First you need to get up to date with api calls

To answer your question as this is a post request


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);
  }
}
  • Related