【问题标题】:Make Get requests with params使用参数发出 Get 请求
【发布时间】:2020-11-23 22:34:49
【问题描述】:

我想用 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 

我尝试编写这个 Typescript 代码:

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 获取结果的正确方法是什么?

【问题讨论】:

    标签: angular typescript typescript1.8 http-request-parameters


    【解决方案1】:

    您正在尝试执行 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();
    

    【讨论】:

    • 我想做一个 GET。我需要如何实现这个?
    • 感谢您的更新。最后一个问题:当我收到http://localhost:9000/callback?code=7CLwgh 的回复时,如何将结果输入pipe() 并获得code 的值?
    • 不确定是否可以,直到响应正文包含相同的值。见stackoverflow.com/questions/36885556/…
    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多