我想在使用Spring Boot完成的后端API中使用端点。端点包括通过电子邮件发送OTP代码。端点在我测试时可以正常工作,但在我的flutter应用程序中打电话给他,出现此错误: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;
});
}
答案 0 :(得分:0)
您正在HTTP正文中发送 email 参数,但是Spring Boot API希望将其作为URL中的查询参数。在这里,您可以看到如何使用Flutter http客户端发送查询参数:How do you add query parameters to a Dart http request?