0

I have a problem with the API request that are type put or post without body, the problem is that is not recognizing the authorization header, this only happens with the request that doesn't have the body, the other post and put with a body on it works fine

I would like to know why this happens

This is my API call without the body, this is the one that experience the problem

 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)
    }

This one works totally fine 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 Answers 2

3

The put method of HttpClient receives 3 parameters (url, body, options) as shown below:

/**
 * 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>;

If you only pass 2 of the 3 parameters, then the method will assume that what you are trying to do is this.http.put(url, body) but, in your case, the right call would be:

this.http.put(this.API_URL+'api/auth/logout/'+idUsuario, null, this.httpOptions2)
Sign up to request clarification or add additional context in comments.

Comments

0

The second parameter to put need to be the body and third the options. Just pass null for the body.

 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)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.