无法在 Angular 中创建 x-www-form-urlencoded POST
Can't make a x-www-form-urlencoded POST in Angular
我有一个可用的 POST 和 GET 服务,没有发现 CORS 等问题。但是,我根本无法模仿在 Postman 中执行的调用(它工作的地方),我唯一能想象的就是我以某种方式设置了 x-www-form-urlencoded 格式不正确。出于某种原因,谷歌搜索示例给了我 lot 的西班牙文和中文网站。但是,从那里复制示例并没有解决任何问题。
getToken(code: string): Observable<any> {
let url = "https://localhost:44301/connect/token";
let payload = {
client_id: "awesome_client",
client_secret: "HakunaMatata",
redirect_uri: "http://localhost:44304/page01",
code: code,
grant_type: "authorization_code"
};
let headers = { "Content-Type": "x-www-form-urlencoded" };
let options = { headers: headers };
return this.http.post<any>(url, payload, options);
}
我知道有一些 所以我尝试了不同的版本(也基于谷歌搜索和示例)。同样的问题。
getToken(code: string): Observable<any> {
...
return this.http.post<any>(
url,
payload.toString(),
{
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
});
}
我根本看不出我错过了什么。邮递员产生预期结果(body/x-www 已启用)。
https://localhost:44301/connect/token
client_id:awesome_client
client_secret:HakunaMatata
redirect_uri:http://localhost:44304/page01
code:4B822...
grant_type:authorization_code
当您使用对象作为 HTTP 客户端的主体时,它会将其序列化为 json。
如果您想将数据作为 x-www-form-urlencoded 发送,您需要使用 HttpParams 作为正文。
getToken(code: string) {
let url = 'https://localhost:44301/connect/token';
let param = {
client_id: 'awesome_client',
client_secret: 'HakunaMatata',
redirect_uri: 'http://localhost:44304/page01',
code: code,
grant_type: 'authorization_code'
};
var payload = new HttpParams({ fromObject: param });
let headers = { 'Content-Type': 'x-www-form-urlencoded' };
let options = { headers: headers };
return this.http.post<any>(url, payload, options);
}
我有一个可用的 POST 和 GET 服务,没有发现 CORS 等问题。但是,我根本无法模仿在 Postman 中执行的调用(它工作的地方),我唯一能想象的就是我以某种方式设置了 x-www-form-urlencoded 格式不正确。出于某种原因,谷歌搜索示例给了我 lot 的西班牙文和中文网站。但是,从那里复制示例并没有解决任何问题。
getToken(code: string): Observable<any> {
let url = "https://localhost:44301/connect/token";
let payload = {
client_id: "awesome_client",
client_secret: "HakunaMatata",
redirect_uri: "http://localhost:44304/page01",
code: code,
grant_type: "authorization_code"
};
let headers = { "Content-Type": "x-www-form-urlencoded" };
let options = { headers: headers };
return this.http.post<any>(url, payload, options);
}
我知道有一些
getToken(code: string): Observable<any> {
...
return this.http.post<any>(
url,
payload.toString(),
{
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
});
}
我根本看不出我错过了什么。邮递员产生预期结果(body/x-www 已启用)。
https://localhost:44301/connect/token
client_id:awesome_client
client_secret:HakunaMatata
redirect_uri:http://localhost:44304/page01
code:4B822...
grant_type:authorization_code
当您使用对象作为 HTTP 客户端的主体时,它会将其序列化为 json。 如果您想将数据作为 x-www-form-urlencoded 发送,您需要使用 HttpParams 作为正文。
getToken(code: string) {
let url = 'https://localhost:44301/connect/token';
let param = {
client_id: 'awesome_client',
client_secret: 'HakunaMatata',
redirect_uri: 'http://localhost:44304/page01',
code: code,
grant_type: 'authorization_code'
};
var payload = new HttpParams({ fromObject: param });
let headers = { 'Content-Type': 'x-www-form-urlencoded' };
let options = { headers: headers };
return this.http.post<any>(url, payload, options);
}