2017-07-07 13 views
1

私はREST API(DJANGO)を呼び出す角度4でアプリを書く。以下のように コードは次のとおりです。角4 GET 403の応答

export class ForecastUnitService { 

private path = 'forecastunit/'; 
private options; 
constructor (private http: Http, private c:CentralService) { 
this.options=this.c.createOptions(); 
} 

getForecastUnits(): Observable<ForecastUnit[]> { 
return this.http.get(this.c.createURL(this.path),this.options) 
    .map(this.extractData) 
    .catch(this.handleError); 
} 
... 
} 

私は、認証ヘッダーが含まれているオプションを追加し、それでも私は禁じられ 403を取得します。しかし、私はcUrlまたは郵便配達を使って、または同じトークンを使ってスワッガーで呼び出しを試みると結果が得られます。 したがって、最初の取り払わ、ヘッダが正しくありませんが、私は私が得る選択肢ログインしたとき:

{"method":null,"headers":{"Authorization":["**Correct Token**"],"Content-Type":["application/json"]},"body":null,"url":null,"withCredentials":null,"responseType":null} 

をだから私はChromeからの応答に見たとき、私は以下を参照してください、問題ではありません。

{"detail":"Authentication credentials were not provided."} 

再度資格情報はありませんが、私は本当にそれらを渡しました、何がうまくいかないのですか?

+0

渡されたトークンの形式は何ですか? – Aniket

+0

基本64、私は – fangio

+0

あなたが渡しているトークンの例のフォーマットを知りたいと思っています認証 – Aniket

答えて

1

Angularのhttpモジュールは、すべてのヘッダー名を小文字に変更します。一部のAPIサービスが仕様に準拠していないため、ヘッダーチェックで大文字と小文字を区別しないと大文字と小文字が区別されるため、問題が発生する可能性があります。

APIの設定を確認してください。

あなたのCentralServiceが有効なオプションを作成するとします。

interface RequestOptionsArgs { 
    url: string|null 
    method: string|RequestMethod|null 
    search: string|URLSearchParams|{[key: string]: any | any[]}|null 
    params: string|URLSearchParams|{[key: string]: any | any[]}|null 
    headers: Headers|null 
    body: any 
    withCredentials: boolean|null 
    responseType: ResponseContentType|null 
} 

ヘッダ:

class Headers { 
    static fromResponseHeaderString(headersString: string): Headers 
    constructor(headers?: Headers|{[name: string]: any}|null) 
    append(name: string, value: string): void 
    delete(name: string): void 
    forEach(fn: (values: string[], name: string|undefined, headers: Map<string, string[]>) => void): void 
    get(name: string): string|null 
    has(name: string): boolean 
    keys(): string[] 
    set(name: string, value: string|string[]): void 
    values(): string[][] 
    toJSON(): {[name: string]: any} 
    getAll(name: string): string[]|null 
    entries() 
} 
+0

私の質問の2番目のコードブロックは私のRequestOptionsです。 – fangio

+0

TypeScriptは、サービスにすべてが正しく入力されていると、考えられるエラーを強調表示する必要があります。場合によっては、[RequestOptionsArgs](https://angular.io/api/http/RequestOptionsArgs)をAngular docsでチェックしてください。 –

+0

ヘッダーチェックを大文字と小文字を区別しないように変更するにはどうすればよいですか?または、Angularで小文字ではないヘッダー? – fangio

-1

はあなたが私たちにCentralServiceを示してもらえますか? コンストラクタでそれを呼び出すのを避け、代わりにOnInitを実装することもできます。

関連する問題