[英]Unsupported Media Type 415, but in Postman works fine
注冊后用戶會得到一個鏈接,鏈接由 userId 和 token 組成。 當用戶點擊它時 - angular項目打開,然后angular從鏈接中獲取userId和token,並發送一個post方法到后端驗證email。
鏈接示例
驗證 email 的 Post 方法:
[HttpPost("confirmEmail/{userId}")]
public async Task<IActionResult> ConfirmEmail(string userId, [FromBody]string token)
{
var codeDecodedBytes = WebEncoders.Base64UrlDecode(token);
var codeDecoded = Encoding.UTF8.GetString(codeDecodedBytes);
var user = await _userManager.FindByIdAsync(userId);
var result = await _userManager.ConfirmEmailAsync(user, codeDecoded);
return Ok(result);
}
在 Angular 中獲取 userId 和 token
userId: string;
token: string;
constructor(private activatedRoute: ActivatedRoute, private authService: AuthService) {
this.activatedRoute.queryParams.subscribe(params => {
this.userId = params['userId'];
this.token = params['token'];
});
}
ngOnInit(): void {
this.confirmEmail();
}
confirmEmail(){
this.authService.confirmEmail(this.userId, this.token).subscribe(data => { console.log("success")});
}
(AuthService) 向后端發送數據
confirmEmail(userId: string, token: string){
console.log(userId);
console.log(token);
return this.http.post(this.authUrl + `confirmemail/${userId}`, token);
}
嘗試將Content-Type
設置為json
類型:
confirmEmail(userId: string, token: string) {
const body = JSON.stringify(token);
const options = {
headers: new HttpHeaders().append('Content-Type', 'application/json')
};
return this.http.post(this.authUrl + `confirmemail/${userId}`, body, options)
.pipe(
map(res => res)
, catchError(this.handleError())
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.