1

I'm currently having trouble with HttpClient vs Axios

when I use this code

   const requestBody = {
      grant_type: 'refresh_token',
      client_id: environment.APP_COGNITO_CLIENT_ID,
      refresh_token: this.storage.get("auth").refresh_token,
    };

    const params = {
      baseURL: "https://testing.auth.eu-west-2.amazoncognito.com",
      url: '/oauth2/token',
      method: 'post',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: qs.stringify(requestBody),
    };

    const result = await axios.request(params);

I can refresh my token

but when I use this code

 try {
      let refresh_token = this.storage.get("auth").refresh_token;
      let access_token = this.storage.get("auth").access_token;
      let queryString = `client_id=${environment.APP_COGNITO_CLIENT_ID}&refresh_token=${refresh_token}&grant_type=refresh_token`;
      const httpOptions = {
        headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }),
        observe: 'response'
    };
      // let result = await lastValueFrom(this.$http.post<any>(test, { headers: headers }));

      let result = await lastValueFrom(this.$http.post<any>(environment.APP_COGNITO_TOKEN_ENDPOINT + '/oauth2/token?' + queryString, httpOptions));
   
      return "Token Refresh Success";
    } catch (error: any) {
      
      return "error";
    }

I get an error Unable to Read response 405 Which I found in the internet that it's probably header problem,

I want to use it the angular way. But it seems the angular itself has a problem.

I tried append, set

In the headers it still won't work. any ideas that help ?

  • maybe it's because the second argument is the body/data and the third are the headers. so it should be like this: `this.$http.post(environment.APP_COGNITO_TOKEN_ENDPOINT + '/oauth2/token?' + queryString, null, httpOptions)` -------- or ad your data instead of `null` – Zerotwelve May 25 '22 at 07:58
  • Look at this answer https://stackoverflow.com/a/39864307/715458. Also, you can pass queryString as an object and HttpClient will convert it. – Bojan Kogoj May 25 '22 at 08:01

1 Answers1

1

maybe it's because the second argument is the body/data and the third are the headers. so it should be like this: this.$http.post(environment.APP_COGNITO_TOKEN_ENDPOINT + '/oauth2/token?' + queryString, null, httpOptions) -------- or ad your data instead of null

By Zero Twelve

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '22 at 21:37