[英]Angular HttpClient post read headers from empty response
我正在尝试在 JWT 上构建我的身份验证。 我正在使用 Spring 过滤器来实现该目标,并且在成功登录时返回带有一个标头(授权)的正面(200)响应。 不幸的是,我的 Angular 客户端仍然遇到 JSON 解析问题。
客户端代码:
this.http.post<HttpResponse<void>>(EndPoint.LOGIN,
{'username': username, 'password': password } ,
{'headers' : new HttpHeaders ({'Content-Type' : 'application/json', 'responseType': 'text'})})
.subscribe(response => console.log(response.headers.get('Authorization')))
;
回应:
authorization →Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJHZXJhbHQiLCJleHAiOjE1MjAwNTk4MzN9.813r4QmVgrnrsm4HwZID1M56hD42PLe0BvbCu-bQoQWSnxllOE0iAjS-fc-BI8R8eGGE0WPSSL0OaKxz2lxDjA
content-length →0
错误:
message : "Unexpected end of JSON input"
它就像来自 Postman 等 REST 客户端的魅力一样工作。
我正在尝试多种方法来完成此操作:
暂时没有运气。
responseType 不是 http 标头,而只是 http 请求的一个选项
如果您希望能够检索标头,您还需要指定observe: response
https://angular.io/guide/http#reading-the-full-response
试试看
this.http.post(EndPoint.LOGIN,
{'username': username, 'password': password } ,
{'headers' : new HttpHeaders ({'Content-Type' : 'application/json'}), 'responseType': 'text', observe:'response'})
.subscribe((response : any) => console.log(response.headers.get('Authorization')))
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.