【发布时间】:2018-08-05 15:10:39
【问题描述】:
我有一个问题,当我需要将用户插入 Keycloak 时,我遇到了这个错误:
消息 : “http://localhost:8080/auth/admin/realms/demo/clients 的 Http 失败响应:415 不支持的媒体类型” 姓名 : “HttpError 响应” 好的 : 错误的 状态 : 415 状态文本 : “不支持的媒体类型” 网址 : "http://localhost:8080/auth/admin/realms/demo/clients"
如果你能帮助我,我给你我的代码:
getToken(tppname) {
const data = new HttpParams()
.set('username', 'pierrecolart')
.set('password', 'root')
.set('grant_type', 'password')
.set('client_id','admin-cli');
console.log(tppname);
token: '';
tokenValue: '';
this.http
.post(
this.ROOT_URL,
data.toString(),
{headers: new HttpHeaders().set('content-type', 'application/x-www-form-urlencoded')}
)
//.map(res => res.json())
.subscribe(data => {
console.log(data);
this.token = data['access_token'];
console.log(this.token);
this.tokenValue = 'Bearer ' + this.token;
const dataPost = new HttpParams()
.set('Client ID', 's');
console.log(this.tokenValue);
this.http
.post(
'http://localhost:8080/auth/admin/realms/demo/clients',
dataPost.toString(),
{headers: new HttpHeaders().set('content-type', 'application/x-www-form-urlencoded').set('Authorization', this.tokenValue).set('Accept', 'application/json')}
).subscribe(data => {
console.log(data); })
})
【问题讨论】:
-
您应该向我们提供您的后端路由配置。 415 肯定意味着您的服务器没有监听请求的 URL 的 POST 方法。
-
我没有在 localhost 中运行,我认为我的问题来自我发送的媒体
-
是的,但接受的内容类型取决于您的后端。您正在设置内容类型“application/x-www-form ....”,但这真的是您的控制器正在等待的吗?你能尝试一个简单的 .post(this.ROOT_URL, data) 而不是 .post( this.ROOT_URL, data.toString(), {headers: new HttpHeaders().set('content-type', 'application/x- www-form-urlencoded')} ) ?
标签: angular httpclient keycloak