为什么授权头不在Angular的API请求中发送?



我有一个问题的API请求类型是put或post没有主体,问题是不识别授权头,这只发生在没有主体的请求,另一个post和put与主体上它工作良好

我想知道为什么会这样

这是我没有主体的API调用,这是遇到问题的那个

removeToken(idUsuario:number,token:any){
this.httpOptions2 = {
headers: new HttpHeaders({
'authorization': token,
})
}
return this.http.put(this.API_URL+'api/auth/logout/'+idUsuario,this.httpOptions2)
}

这个完全可以工作https://www.screencast.com/t/OfyOvxXn

finishStudy(idStudio:number,resultado:string,token:any){
this.httpOptions2 = {
headers: new HttpHeaders({
'authorization': token,
})
}
return this.http.put(this.API_URL+'api/analista/finalizar/'+idStudio,{"resultado":resultado},this.httpOptions2)
}

HttpClientput方法接收3个参数(url, body, options),如下所示:

/**
* Constructs a `PUT` request that interprets the body as a text string and
* returns the response as a string value.
*
* @param url The endpoint URL.
* @param body The resources to add/update.
* @param options HTTP options
*
* @return An `Observable` of the response, with a response body of type string.
*/
put(url: string, body: any | null, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'text';
withCredentials?: boolean;
}): Observable<string>;

如果您只传递3个参数中的2个,那么该方法将假设您要做的是this.http.put(url, body),但是,在您的情况下,正确的调用将是:

this.http.put(this.API_URL+'api/auth/logout/'+idUsuario, null, this.httpOptions2)

第二个要放置的参数是body,第三个是options。为body传递null。

removeToken(idUsuario:number,token:any){
this.httpOptions2 = {
headers: new HttpHeaders({
'authorization': token,
})
}
return this.http.put(this.API_URL+'api/auth/logout/'+idUsuario, null, this.httpOptions2)
}

最新更新