angular - 使用参数发出 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
?
解决方案
您正在尝试执行 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();
推荐阅读
- javascript - 否则 if 语句有效,但似乎令人困惑
- json - “无法反序列化当前 JSON 对象”如何解决此错误?
- javascript - 打字稿,对象的类型数组。只需要一个对象的属性设置为 true
- google-apps-script - 如何在 Google Apps 脚本中解析这个对象?
- javascript - 通过跨度创建点不是从css中的反应生成的
- python - 使用 50x50 阵列的慢速 keras 拟合方法,我怎样才能让它更快?
- prolog - swipl - prolog 将所有成员加入列表
- java - 在 webService 上调用 jni4net 时出错,URI 方案不是“文件”
- javascript - 在 TypeScript 中使用“替代模式”混合
- python - 如何分发 Python 虚拟环境?