Make Get requests with params

198 Views Asked by At

I want to write in Angular Typescript this request:

http://localhost:8080/engine/oauth/authorize?client_id=admin&redirect_uri=http://localhost:9000/callback&response_type=code&scope=read_profile

When I open this link into the web browser I get redirection response:

http://localhost:9000/callback?code=7CLwgh 

I tried to write this Typescript code:

authCodeRequest(name: string, password: string): Observable<any> {
    const body = new HttpParams()
      .set('client_id', 'admin')
      .set('redirect_uri', 'http://localhost:9000/callback')
      .set('response_type', 'code')
      .set('scope', 'read_profile');
    const headers = new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
    });

    return this.httpClient
      .get(environment.api.urls.auth.token, body.toString(), {
        headers
      })
      .pipe(
        map((response: Authorize) => {
          console.log(data)
          return true;
        })
      );
  }

I get this error for the get request: TS2554: Expected 1-2 arguments, but got 3.

What is the proper way to make a request and get the result from the redirected link param code?

1

There are 1 best solutions below

3
On

You are trying to execute a POST, but you are actually executing a GET. Replace httpClient.get with httpClient.post.

If you really want to do a GET, you cannot use x-www-form-urlencoded. Your request parameters will be added to the request url.

let params= new HttpParams()
  .set('client_id', 'admin')
  .set('redirect_uri', 'http://localhost:9000/callback');
let options = { params: params };
return this.httpClient
  .get(environment.api.urls.auth.token, options)
  .pipe();