Pierre Colart
Pierre Colart

Reputation: 175

Angular/Keycloak : 415 Unsupported Media Type

I have a problem, when I need to insert user to Keycloak I have this error :

message : "Http failure response for http://localhost:8080/auth/admin/realms/demo/clients: 415 Unsupported Media Type" name : "HttpErrorResponse" ok : false status : 415 statusText : "Unsupported Media Type" url : "http://localhost:8080/auth/admin/realms/demo/clients"

I give you my code if you can help me :

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); })
  })

Upvotes: 4

Views: 8741

Answers (1)

Sean Kelleher
Sean Kelleher

Reputation: 2102

As Pierre Mallet mentioned in the comments, the Content-Type you're using is application/x-www-form-urlencoded. This can be used for logins, e.g. when you're requesting openid-connect/token. However, this Content-Type doesn't seem to be supported by the admin API currently, but you can make the request with a Content-Type of application/json, which should give you a 201 response assuming you provide valid user credentials.

Upvotes: 4

Related Questions