【发布时间】:2017-06-18 21:12:01
【问题描述】:
我正在学习 Angular,作为其中的一部分,我正在尝试了解如何实现我想到的用例。此时,我正在尝试测试服务器通信。即,RxJs 的 Observables。
我想到的用例是发送一个 POST 请求,该请求需要在服务器端接受 X-XSRF-TOKEN 标头。这是第一次发送 GET 请求时作为 cookie 发送回客户端的安全标头。换句话说,与服务器的任何对话都不能以 POST 请求开始。
我希望实现这一点的方式是包装 Angular 的 Http 类:
import {Http, RequestOptions, RequestOptionsArgs, Response} from '@angular/http';
import { Injectable} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { CookieService } from 'angular2-cookie/services/cookies.service';
@Injectable()
export class RestClient {
private static xsrfTokenCookie: string = 'XSRF-TOKEN';
private static xsrfTokenHeader: string = 'X-XSRF-TOKEN';
private static xsrfToken: string = null;
constructor(private http: Http, private cookies: CookieService) {}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.http.get(url, options);
}
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
if (!options) {
options = new RequestOptions(new Headers());
}
if (!RestClient.xsrfToken) {
RestClient.xsrfToken = this.cookies.get(RestClient.xsrfTokenCookie);
}
if (!RestClient.xsrfToken) {
//TODO: Somehow construct an Observable object which once subscribed, it will make a GET request
// then using the returned cookie, it will construct the actual POST request.
}
}
}
所以我的问题是,我怎样才能编写一个 POST 请求,它首先发送一个 GET 请求,等待它的响应,然后它会发送实际的 POST 请求。当然,post 方法的调用者会得到的返回的 Observable 属于 POST 请求,而不是 GET 请求。所以如果我写:
restClient.post('/', {}).subcribe(response => {
//It's POST's response
})
【问题讨论】:
标签: angular rxjs angular2-http