spring-boot - 如何在颤动的端点调用中正确放置参数?
问题描述
我想在使用 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;
});
}
解决方案
您在 HTTP 正文中发送电子邮件参数,但 Spring Boot API 期望它作为 URL 中的查询参数。在这里你可以看到如何使用 Flutter http 客户端发送查询参数:How do you add query parameters to a Dart http request?
推荐阅读
- javascript - 无法将 React 的状态挂钩保存到数据库中,即使使用 useEffect
- kubernetes - Kubernetes Missing secret file with error Error: secret "env" not found
- html - css flexbox 和高度自动
- reactjs - 第一次选择未定义以 redux 形式选择表单值
- mysql - 在带值 X 和效率的行之后选择带值 Y 的行
- c# - 使用官方 .NET SDK 在 linux 上通过 cli 编译 c# 代码
- time-series - 多年重复一年时间序列
- reactjs - Why there is no icons of fullscreen on my map?
- javascript - NaN 有问题。试图修复它,添加了一个 gazillion parseInts 和 parseFloats 来尝试修复它,没有工作
- typescript - How can I exported a type from a Vue 3 single file Typescript component?