2
votes

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); })
  })
1
You should provide us your backend routing configuration. The 415 certainly means that your server is not listening on POST method for the requested URL.Pierre Mallet
I do not have it all is running in localhost, I think my problem come from the media that I sentPierre Colart
yeah but the accepted content type depend of your backend. You are setting a content type "application/x-www-form ...." but is this really what your controller is waiting for ? can you try a simple .post(this.ROOT_URL, data) instead of .post( this.ROOT_URL, data.toString(), {headers: new HttpHeaders().set('content-type', 'application/x-www-form-urlencoded')} ) ?Pierre Mallet

1 Answers

1
votes

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.