【问题标题】:Angular/Keycloak : 415 Unsupported Media TypeAngular/Keycloak:415 不支持的媒体类型
【发布时间】: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


【解决方案1】:

正如 Pierre Mallet 在 cmets 中提到的,您使用的 Content-Typeapplication/x-www-form-urlencoded。这可用于登录,例如当您请求openid-connect/token 时。但是,管理员 API 目前似乎不支持此 Content-Type,但您可以使用 Content-Typeapplication/json 发出请求,假设您提供有效的用户凭据,这应该会给您一个 201 响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多