问题
In a project, we used both Http & HttpClient to fetch the header params. Http returns the header params, but HttpClient doesn't.
constructor(private http: Http, private httpClient: HttpClient) {}
getContent() {
const url = '';
return this.http.post(url, data)
.map((res: HttpResponse<any>) => {
console.log('http content', res);
});
return this.httpClient.post(url, data, { observe: 'response' })
.map((res: HttpResponse<any>) => {
console.log('httpClient content',res);
});
}
When checked in console, http returns the response with headers but httpClient returns an empty array in headers.
When checked in networks tab of browser inspector, it displays all the header parameters.
The server accepts the following CORS options:
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Origin,X-Requested-With,x-access-token,Content-Type,Authorization,Accept,jwt',
exposedHeaders: 'Content-Type,Authorization,jwt'
Please help me figure out how to fetch the headers using httpClient.
Thanks in advance.
回答1:
You are on the right track with the HttpClient. You should use the get()
method on your headers.
this.httpClient.post(url, data, {observe: 'response'})
.map((res: HttpResponse<any>) => {
let myHeader = res.headers.get('my-header');
return res;
});
Where my-header
is the header response you are trying to fetch.
来源:https://stackoverflow.com/questions/47389365/httpclient-can%c2%b4t-access-to-response-headers