Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DioError [DioErrorType.RESPONSE]: Http status error [404]

Postman form-data request

Request body containing

Expected Response body

Dio Flutter code

import 'package:dio/dio.dart';
import 'package:flutter_project/config/config.dart';
import '../model/postImage.dart';
import 'dart:io';



Future<PostImage> postImage(File image) async{
  String fileName = image.path.split('/').last;
  print(fileName);

  try{
    Dio dio = new Dio();
    FormData formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(image.path,filename: fileName)
    });
    Map<String, String> headers= <String,String>{
      'Content-Type':'multipart/form-data'
    };
    print("${baseURL}files/upload");
    Response response = await dio.post("${baseURL}files/upload",data: formData);
    if(response.statusCode == 200){
      print("Uploaded");
    }
    else{
      print(response.data);
    }
    
  }
  catch(e){
      print(e);
  }

}

Expection that I am getting : DioError [DioErrorType.RESPONSE]: Http status error [404]

like image 792
Nafiz Rahman Avatar asked Jul 11 '26 14:07

Nafiz Rahman


1 Answers

I added contentType: new MediaType("image", "jpeg")//in the formData. It works now.

Full Code :

import 'package:dio/dio.dart';
import 'package:flutter_project/config/config.dart';
import '../model/postImage.dart';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';

Future<PostImage> postImage(File image) async {
  String fileName = image.path.split('/').last;
  print(fileName);

  try {
    Dio dio = new Dio();
    FormData formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(
        image.path,
        filename: fileName,
        contentType: new MediaType("image", "jpeg"),
      )
    });
    Map<String, String> headers = <String, String>{
      'Content-Type': 'multipart/form-data'
    };
    print("${baseURL}files/upload");
    Response response =
        await dio.post("${baseURL}files/upload", data: formData);
    if (response.statusCode == 200) {
      print("Uploaded");
    } else {
      print(response.data);
    }
    print('Out');
  } catch (e) {
    print(e);
  }
}

like image 55
Nafiz Rahman Avatar answered Jul 14 '26 14:07

Nafiz Rahman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!