首页 > 解决方案 > 如何在颤动的端点调用中正确放置参数?

问题描述

我想在使用 Spring Boot 完成的后端 API 中使用端点。端点包括通过电子邮件发送 OTP 代码。端点在我测试时工作正常,但我在我的颤振应用程序中调用他,我有这个错误:Required String is not present 这是端点代码的一部分:

        @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", 
        value = "Bearer bcryptjwttoken", paramType = 
         "header",required=true),@ApiImplicitParam(name = "Content-Type", 
         value = "application/json", paramType = 
         "header",required=true),
        @ApiImplicitParam(name = "Accept", value = "application/json", 
        paramType = "header",required=true)
         })
       @PostMapping( "/otp/request/{email}")
       public  ResponseEntity<Object> requestOtpPassword(@RequestParam String 
       email) throws AbysterpubFunctionalException {
         try {
          Utilisateur out = userService.requestOtpPassword(email);
         System.out.println("ramses");
         return ResponseEntity.ok(out);
          } catch( AbysterpubFunctionalException afe) {
           return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body(new ErrorMessage(afe.getMessage()));

         }
          catch (Exception ex) {
                return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new ErrorMessage(ex.getMessage()));          
           }

          }

这是我尝试调用端点的颤振代码:

           Future<void> requestOtp(String email) {
          RestClient.getAuthToken().then((String token) {
           Map<String, dynamic> postData = {
          "email": email,
           };
          RestClient.procesPostRequest("user/otp/request/$email", postData, 
         token)
         .then((response) {
         print(response);

        if (response == 200) {
        isOTPsent = true;
        } else {
        isOTPsent = false;
        }
       });
       }).catchError((onError) {
       print('$onError');
       return Future.value('error');
      });
      }

          static Future<int> procesPostRequest(String path, Map<String, 
      dynamic> postData,String 
           token) {
        Map<String, String> headers = RestClient.getHeaders(token);

       return http.post("$WS_URL/$path", body: json.encode(postData), 
       headers: headers)
      .then((http.Response response) {
       if (response.statusCode != 200) {
       print(
        "Error while processing post request \n 
       ${WSErrorHandler.fromJson(json.decode(response.body)).toJson()}");
        }
       return response.statusCode;
       });
       }

标签: spring-bootflutter

解决方案


您在 HTTP 正文中发送电子邮件参数,但 Spring Boot API 期望它作为 URL 中的查询参数。在这里你可以看到如何使用 Flutter http 客户端发送查询参数:How do you add query parameters to a Dart http request?


推荐阅读