在发出POST请求时添加需要发送的头部信息。
示例代码:
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
const data = {
name: 'John',
email: 'john@gmail.com'
};
this.http.post('https://example.com/api/user', data, httpOptions)
.subscribe(response => console.log(response));
在示例代码中,httpOptions
包含了需要发送的头部信息,它被传递给post()
方法的第三个参数。data
包含了要发送的数据。这样,当POST请求被发送时,头部信息也会被发送。