Chung
Chung

Reputation: 165

Http status error 413 when using dio package

When I use Dio package in Flutter, I request on server with FormData and authenticate with Token. And I get an error: "DioError [DioErrorType.response]: Http status error [413]". It seems I send a large query. Which result for this problem? Help me!
Thanks in advance!

Updated: I find the reason. It is "When ListFile has greater than 1, 2 parameter for image => Http status error [413]". But I don't know to find the resolve.

Code:

Future createPost(FormPostingArgument? argument) async {
    Map<String, dynamic> param = {};
    param['plan_id'] = argument?.planId;
    param['is_advance_notice_required'] = argument?.isAdvancedNoticeRequired;
    for (int index = 0; index < argument!.listFile.length; index++) {
      if (argument.listFile[index].typeFile == TypeFile.IMAGE) {
        ///type 1: Image, 2: Video
        param['image_or_video[$index][type]'] = 1;
        param['image_or_video[$index][image]'] = await MultipartFile.fromFile(
          argument.listFile[index].pathFile,
          filename: 'File$index',
          contentType: MediaType("image", argument.listFile[index].pathFile.split(".").last),
        );
      } else {
        param['image_or_video[$index][type]'] = 2;
        param['image_or_video[$index][video]'] = await MultipartFile.fromFile(
          argument.listFile[index].pathFile,
          filename: 'File$index',
          contentType: MediaType("video", argument.listFile[index].pathFile.split(".").last),
        );
      }
    }
    param['subcategory_id'] = 1;
    param['title'] = argument.title;
    param['body'] = argument.content;
    param['prefecture_cd'] = argument.prefecture?.prefectureId;
    param['city'] = argument.district;
    if (checkNullText(argument.address))
      param['street_number'] = argument.address;
    if (checkNullText(argument.buildingName))
      param['building_name'] = argument.buildingName;
    if (checkNullText(argument.numberOfPeople))
      param['num_of_people'] = int.parse(argument.numberOfPeople!);
    if (checkNullText(argument.gender))
      param['sex'] = getParamGender(argument.gender);
    if (checkNullText(argument.ageLower!))
      param['age_lower'] = int.parse(argument.ageLower!);
    if (checkNullText(argument.ageUpper))
      param['age_upper'] = int.parse(argument.ageUpper!);
    param['start_time'] = argument.startTime;

    var _dio = Dio();
    String? token = await LocalStorage().getToken();
    _dio.interceptors.add(InterceptorsWrapper(onRequest: (request, handler) {
      request.headers["Authorization"] = "Bearer $token";
      return handler.next(request);
    }));
    var response = await _dio
        .post(
      NetworkConfigure.BASE_URL + NetworkConfigure.API_NAME + "posts",
      data: FormData.fromMap(param),
    )
        .catchError((onError) {
      print("Error: $onError");
    });
    print("Result: ${response.data}");
  }

Upvotes: 0

Views: 2654

Answers (1)

Suganya
Suganya

Reputation: 439

Http status code 413 happens when your request entity is too large for the server. When you are trying to upload file [image / video / document]. Confirm the below points

  1. if the server has specified the upload file limit size, then try to post with the size or else it return 413 status code.
  2. if it is not specified in server, then by default it taken as 1mb

Try to post image or video less than 1mb, if you got success response ask to increase the upload size in the server.

Upvotes: 2

Related Questions