简体   繁体   English

Angular HttpClient 从空响应中发布读取标头

[英]Angular HttpClient post read headers from empty response

I'm trying to build my authentication on JWT.我正在尝试在 JWT 上构建我的身份验证。 I'm using Spring filters to achieve that goal, and i'm returning positive (200) response with one header (Authorization) on successfull login.我正在使用 Spring 过滤器来实现该目标,并且在成功登录时返回带有一个标头(授权)的正面(200)响应。 Unfortunately i'm still getting JSON parse problem from my Angular client.不幸的是,我的 Angular 客户端仍然遇到 JSON 解析问题。

Client code:客户端代码:

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')))
  ;

Response:回应:

authorization →Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJHZXJhbHQiLCJleHAiOjE1MjAwNTk4MzN9.813r4QmVgrnrsm4HwZID1M56hD42PLe0BvbCu-bQoQWSnxllOE0iAjS-fc-BI8R8eGGE0WPSSL0OaKxz2lxDjA
content-length →0

Error:错误:

message : "Unexpected end of JSON input"

It is working like a charm from REST client like Postman.它就像来自 Postman 等 REST 客户端的魅力一样工作。

I was trying multiple things to get this done:我正在尝试多种方法来完成此操作:

  • set responseType to text将 responseType 设置为文本
  • add something to response body (error has changed - it can't parse first letter of new response body)向响应正文添加一些内容(错误已更改 - 它无法解析新响应正文的第一个字母)
  • posting without generic HttpResponse object没有通用 HttpResponse 对象的发布

No luck for now.暂时没有运气。

responseType is not a http header, but just an options for the http request responseType 不是 http 标头,而只是 http 请求的一个选项

You also need to specify observe: response if you want to be able to retrieve the header如果您希望能够检索标头,您还需要指定observe: response

https://angular.io/guide/http#reading-the-full-response https://angular.io/guide/http#reading-the-full-response

Try that试试看

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.

相关问题 c# httpclient 无法读取发布响应为 json - c# httpclient unable to read post response as json 从 Angular Observable 中的响应中删除标头 - Removing headers from response in Angular Observable 如何发布(参数)并从android中的Httpclient和Httppost获取响应 - how to post (parameters) and get the response from Httpclient and Httppost in android 将来自 httpclient Post 请求的 JSON 响应反序列化到自定义 object - Deserialising a JSON response from a httpclient Post request into a custom object POST时HttpClient无法获得响应 - HttpClient not getting response when POST 如何从api响应Angular HttpClient格式化数据 - How to format data from api response Angular HttpClient 使用php API从angular6的httpClient响应中检索参数值? - Retrieving a parameter value from an httpClient response in angular6 with a php API? Angular:HttpClient 错误,响应为空 200/201(总是调用 JSON.parse(&quot;&quot;)) - Angular: HttpClient error with empty 200/201 response (always calls JSON.parse("")) 如何从 jquery 中的 post 请求中读取响应? - How read response from a post request in jquery? 带有PHP服务器响应的HttpClient JSONData POST请求 - HttpClient JSONData POST Request with Response for PHP Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM