首页 > 解决方案 > 使用参数发出 Get 请求

问题描述

我想用 Angular Typescript 写这个请求:

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

当我在网络浏览器中打开此链接时,我会收到重定向响应:

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

我试着写这个打字稿代码:

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

我在获取请求时收到此错误:TS2554: Expected 1-2 arguments, but got 3.

发出请求并从重定向链接参数获取结果的正确方法是什么code

标签: angulartypescripttypescript1.8http-request-parameters

解决方案


您正在尝试执行 POST,但实际上您正在执行 GET。将 httpClient.get 替换为 httpClient.post。

如果你真的想做一个 GET,你不能使用 x-www-form-urlencoded。您的请求参数将被添加到请求 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();

推荐阅读