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

时间:2021-02-10 23:43:53

标签: angular typescript

我对类型为 put 或 post without body 的 API 请求有问题,问题是无法识别授权标头,这只发生在没有正文的请求中,另一个 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)
    }

2 个答案:

答案 0 :(得分:0)

要放置的第二个参数需要是正文,第三个是选项。 只需为正文传递 null。

y

答案 1 :(得分:0)

fetch(url) .then(res => { return res.text(); }) .then(body => { const urls = body.match(urlRegex()); const baseUrl = urls.find(url => url.includes("playlist.m3u8")) let liveUrl = baseUrl; liveUrl = liveUrl.replace("pull-hls", "pull-flv"); liveUrl = liveUrl.split("/").slice(0, 5).join("/") liveUrl += ".flv" console.log(`Found live playlist (m3u8) URL: ${baseUrl}`); console.log(`Found live flv URL: ${liveUrl}`); console.log(`Writing live to output.flv. Press Ctrl C to STOP.`) const file = fs.createWriteStream('output_path/output.flv') https.get(liveUrl, function (response) { response.pipe(file); }); fs.writeFile('output_path/completion_check.txt', 'Hello World!', function (err) { if (err) return console.log(err); console.log('completion_check.txt has been created'); }); }) .catch((error) => { console.log(error) }); put 方法接收 3 个参数(url、body、options)如下图:

HttpClient

如果您只传递 3 个参数中的 2 个,则该方法将假定您尝试执行的是 /** * 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>; 但在您的情况下,正确的调用是:

this.http.put(url, body)
相关问题