HTTP POST with Json on Body - Flutter/Dart, The response is empty

215 Views Asked by At

I am making an api call to my server via my flutter app,This is my code to make a request to an API:

import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutterbutyle/models/models.dart';
import 'package:flutterbutyle/repository/resources.dart';
import 'package:flutterbutyle/repository/response/response_body_provider.dart';
import 'package:flutterbutyle/repository/service_numbers.dart';
import 'package:http/http.dart' as http;

class ProviderApiClient {
  final http.Client httpClient;

  ProviderApiClient({@required this.httpClient}) : assert(httpClient != null);

  Future<List<Provider>> getAllProviders(
      int startRowIndex, int recordCount) async {


    Map map = {
      'data': {
        'sn': getAllProvidersSN,
        'p27': startRowIndex,
        'p28': recordCount,
      },
    };


    final response = await httpClient.post(postApiUrl, headers: {
        HttpHeaders.authorizationHeader: token,
        HttpHeaders.contentTypeHeader:'application/json'
      }, body:json.encode(map));



        if (response.statusCode == 200) {
          final data = json.decode(response.body) as ResponseBodyProvider;

          if (data.hasError) {
            throw Exception(data.error.message);
          }

          else {
            return data.providers.map((rawProvider) {
              return Provider.fromJson(rawProvider);
            }).toList();
          }
        }

        else {
          throw Exception('error fetching posts');
        }

  }

}

the body response came empty and i think its because the body i sent on the request. I tested my request first on the Api sandbox website, and then in Postman. It works well there.

0

There are 0 best solutions below