Dio Error : Http Status Error Code [ 405 ] Flutter

Dio Error : Http Status Error Code [ 405 ] Flutter

我在从真实设备发送 post 请求时收到 HTTP 状态错误代码 405,但它在 postman 内部工作。

URL是:https://amapp.adtestbed.com/api/post-survey

以下是我使用的表单数据

FormData formData = FormData.fromMap({
      
        'user_id': id,
        'is_collection':
            jobDetailsResponseModel.jobData.jobStatus == '8' ? 1 : 0,
        'job_id': jobDetailsResponseModel.jobData.id,
        'job_no': bookingIdController.text.toString(),
        'sender_name': senderNameController.text.toString(),
        'reciever_name': recieverNameController.text.toString(),
        'sender_phone': senderPhoneController.text.toString(),
        'reciever_phone': recieverPhoneController.text.toString(),
        'sender_address': senderAddressController.text.toString(),
        'reciever_address': recieverAddressController.text.toString(),
        'sender_signature_data':
            "data:image/jpeg;base64," + base64Imagesendersign,
        'receiver_signature_data':
            "data:image/jpeg;base64," + base64Imagerecieversign,
        'location': currentLocation,
      
      'images[]': uploadimages,
      'maked[]': makedlist,
      'model[]': modallist,
      'rego[]': regolist,
      'speedo[]': speedolist,
      'is_drivable[]': isdrivablelist,
      'goods_inside[]': goodsinsidelist,
      'external_condition[]': externalconditionlist,
      'interior_condition[]': internalconditionlist,
      'survey_image[]': surveyimagelist,
      'comments[]': commentlist
    }); 

这里的图片字段是一个多部分表单列表。 并按如下方式执行请求

final response = await Dio().request(
        'https://amapp.adtestbed.com/api/post-survey',
        data: formData,
        options: Options(
          headers: {
            "Accept": "application/json",
          },
          method: 'POST',
        ),
      );

也用了下面的代码来检查还是不行

final response = await Dio().post(
        'https://amapp.adtestbed.com/api/post-survey',
        data: formData,
        options: Options(
          // headers: {
          //   // "Accept": "application/json",
          //   // 'Content-Type': 'application/x-www-form-urlencoded'
          // },
          method: 'POST',
        ),
      );

当我发送没有图像字段的表单时,它会完美提交并给出 200 状态,但是当我发送它时,它带有包含多部分表单列表的图像字段,然后它返回 405 状态代码.结束报错get method not supported only support post method。我还检查了后端代码,但从那方面没有错误,因为它在发送请求之前给出了错误。我还检查了是否有一些空数据导致了错误,但没有一个字段是空的。

但是当我在 postman 中执行这两种情况时,它工作正常。

这是post人 link:

https://drive.google.com/file/d/1hTmDC2aQ7vTMxSnzwaeRqd9gv7UbPWhR/view?usp=sharing

这些是post男人

的一些图片

检查一下我之前也遇到过类似的问题,我还注意到您没有将 authorization token 添加到 header,这可以解释为什么您得到 405这意味着您无法访问。

我试图在我这边重现你的问题,但授权令牌已过期,我收到 401,但这是代码:

    void postSome() async {
        String base64 = 
      
//This base64 is shortened     "data:image/png;base64,...+WZm/et53efvWO6Tuc6rH7kUXy1lxs+/61t7dMg6VAvURAAAAQHMQogDAbE6dlLXtft/F3cuvkJNbetbn7VWrpUUX+mvEtqdHvgAAAABoOf8P3x1uDPELl4UAAAAASUVORK5CYII=";
        FormData formData = FormData.fromMap({
          'user_id': 139,
          'is_collection': "1" == '8' ? 1 : 0,
          'job_id': "248",
          'job_no': "60036JL#1",
          'sender_name': "test",
          'reciever_name': "test",
          'sender_phone': "1234567890",
          'reciever_phone': "1234567890",
          'sender_address': "test",
          'reciever_address': "test",
          'sender_signature_data':
             base64,
          'receiver_signature_data': base64,
          'location': "32, Gopal Nagar, Om Nagar, Gopal Nagar Society, Parvat Patiya, Surat, Gujarat 395010, India\n",
          'images[]': "/D:/App/Logicwind/GitLAb/whiteboard-digitization/test_images/w11.jpg",
          'maked[]': "test",
          'model[]': "test",
          'rego[]': "test",
          'speedo[]': "123",
          'is_drivable[]': "1",
          'goods_inside[]': "1",
          'external_condition[]': "3",
          'interior_condition[]': "3",
          'survey_image[]': base64,
          'comments[]': "test"
        });
    
        final response = await Dio().request(
          'https://amapp.adtestbed.com/api/post-survey',
          data: formData,
          options: Options(
            headers: {
              'Accept': "application/json",
              'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIzIiwianRpIjoiNTlkNzNlZjE4YzcxNGJjMWM1ZjE0ZGQxOWE1OWZmNmQ1YjVhNDViNDIwNzlkODY4ZmJkOGRmNjJlOGM4MDQzNmVjYmIxZjc4MjM5MmZiOGIiLCJpYXQiOjE2MjgxNzY5NTAsIm5iZiI6MTYyODE3Njk1MCwiZXhwIjoxNjU5NzEyOTUwLCJzdWIiOiI0NiIsInNjb3BlcyI6W119.lfnzUvq_LwYtBUQ-t6S_mKXEoM6er6ZzCuTUoHnqz9wwrdLOcu6x9CEixqMRvI-RWtSoiMO5KCYZsgagayGW5slYk6zAAa0V5fCjkRipqqus6mXe6nowtcXs_2V9ucjVoH4Evkb9lFBlE1rlpxKgcGXTcx4UgQs8kjJ5Wm4A8omedza_hUQPN9KUAfhhBDeL9hq-DkC7QbLu_YxnG5g-AETKhbJu8T4HWKqkh9NKGbNlvs1oi_nD81F1w8uSteY-UOgDJyUoGJxzoD6xmEv3J4mkrdr1ZbG88EzTyk3zngwBp9eWcLHhMS2psxArOnOnWusweJJ5uZiWiu2VIL7fWmV5b6G4OCvvkba5eXmw8iAvACACuCGQtOcGdN_euUgyfM9z9a8QbK8M2I-ux4GGY0ejCamFEJZCTwyBCxmLIrP4XjQoeL71WT1YBOgSZwDjA6qrAfL_fLwbEYQYbLIF8fLUmDOWRKBIaJLSVNG2x0Gl5LLXTiTZAzRRtJlSxZ4-Vid4hQJX39lohp-47XPAxvHnNTxiLw_aAG3SruFZ_rtdX4xnNbV3hSI825CD5dBeQC5iE-hiLXDWDXelM_q2fSD1rez62XQZGp1KfvlwowxAWgIghTfSH_E52h6mdoHEo9q73UJNYDz_F9Sj8EOfY3VWtmN6HMmcs4HZht36qX4',
            },
            method: 'POST',
          ),
        );
        print(response.statusCode);
      }

看起来下面两个字段是文件而不是表单数据。

'sender_signature_data': "data:image/jpeg;base64," + base64Imagesendersign
'receiver_signature_data': "data:image/jpeg;base64," + base64Imagerecieversign

您应该像这样将它们添加为文件字段


"sender_signature_data": await MultipartFile.fromFile(sender_signature_path,
      filename: fileName, contentType: MediaType(mimee, type))
  }),
"receiver_signature_data":await MultipartFile.fromFile(reciever_signature_path,
      filename: fileName, contentType: MediaType(mimee, type))
  })