TEST TEST MAN
TEST TEST MAN

Reputation: 33

Issue with Cognito API using Angular HttpClient and HttpHeader

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 ?

Upvotes: 1

Views: 234

Answers (1)

TEST TEST MAN
TEST TEST MAN

Reputation: 33

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

Upvotes: 1

Related Questions