发布请求,表单数据混乱

时间:2020-03-01 23:25:01

标签: flutter post form-data

我正在尝试将请求发布到仅接受form-data的服务器。首先,我尝试了一个发帖http的请求。另外,尝试使用dio库来实现此目的,因为我认为这对于尝试实现的目标来说是最好的。但是,由于没有收到错误消息,我无法使其正常工作。

Postman sample

其中一个字段接受用户使用文件选择器选择的文件图像对象。 我尝试了两种方法来实现这一目标。

第一条路

void finshUpdate() async{
    if(_image!=null){
      setState(() {
        _isLoading = true;
      });
        SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
        var uri = NetworkUtils.host +
            AuthUtils.updateSessionRequest;
        Map data = {"_method": "PATCH",
          "first_name": widget.first_name,
          "last_name": widget.last_name,
          "phone": widget.phone,
        "industry":widget.industry,
        "country": widget.country,
        "state": widget.state,
        "fav_quote": widget.fav_quote,
        "bio_interest": widget.bio_text,
        "terms": 1,
        "company": widget.company,
        "position": widget.job_position,
        "linked_in":widget.linkedin_profile,
        "institution": widget.institution,
        "degree": widget.degree,
        "image": _image,
        "preference[0]": widget.industry};
        String jsonString = json.encode(data); // encode map to json
        String authToken = sharedPreferences.getString("token");
        try {
          final response = await http.post(
            uri,
            body: jsonString,
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'form-data',
              'Authorization': 'Bearer ' + authToken,
            },
          );

          final responseJson = json.decode(response.body);
          print(responseJson.toString());
          if (response.statusCode == 200 || response.statusCode == 201) {
            setState(() {
              Navigator.pushReplacement(
      context, SlideFromRightPageRoute(widget: DashBoardPage()));
              NetworkUtils.showToast("Profile successfully update!");
            });
          } else{
            setState(() {
              _isLoading = false;
            });
            NetworkUtils.showSnackBar(_scaffoldKey, 'An error occurred. Please try again');
          }
          return responseJson;
        } catch (exception) {
          print(exception.toString());
          setState(() {
            _isLoading = false;
          });
          NetworkUtils.showSnackBar(_scaffoldKey, 'An error occurred. Please try again');
        }
    } else{
      NetworkUtils.showSnackBar(_scaffoldKey, "Please select a profile image");
    }
  }

第二种方式

 void finshUpdate() async {
        if (_image != null) {
          setState(() {
            _isLoading = true;
          });
          FormData formData = new FormData.fromMap({
            "_method": "PATCH",
            "first_name": widget.first_name,
            "last_name": widget.last_name,
            "phone": widget.phone,
            "industry": widget.industry,
            "country": widget.country,
            "state": widget.state,
            "fav_quote": widget.fav_quote,
            "bio_interest": widget.bio_text,
            "terms": 1,
            "company": widget.company,
            "position": widget.job_position,
            "linked_in": widget.linkedin_profile,
            "institution": widget.institution,
            "degree": widget.degree,
            "image": _image,
            "preference[0]": widget.industry
          });
          Response response;
          Dio dio = new Dio();
          response = await dio.post(
              NetworkUtils.host + AuthUtils.endPointUpdateProfile, data: formData);
          print(response.toString());
          print(response.statusCode);
          if (response.statusCode == 200 || response.statusCode == 201) {
            setState(() {
              Navigator.pushReplacement(
                  context, SlideFromRightPageRoute(widget: DashBoardPage()));
              NetworkUtils.showToast("Profile successfully update!");
            });
          } else {
            setState(() {
              _isLoading = false;
            });
            NetworkUtils.showSnackBar(
                _scaffoldKey, 'An error occurred. Please try again');
          }
          setState(() {
            _isLoading = false;
          });
          NetworkUtils.showSnackBar(
              _scaffoldKey, 'An error occurred. Please try again');
        } else {
          NetworkUtils.showSnackBar(_scaffoldKey, "Please select a profile image");
        }
      }

我在做什么错了?

0 个答案:

没有答案