I'm trying to call a local rest webservice to test a simple login function I have tried with a Get function and it works fine now I'm trying with Post function and I have no idea how to pass parameters to my request
import { HttpClient,HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Storage} from "@ionic/storage";
// import { HttpParams } from '@angular/http';
/*
Generated class for the RestProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class RestProvider {
apiUrl = 'http://localhost:8080/CrunchifyTutorials/api/crunchifyService';
user : any;
constructor(public http: HttpClient, public storage: Storage) {
console.log('Hello RestProvider Provider');
//console.log(this.storage.get('user'));
}
// getUsers() {
// return new Promise(resolve => {
// this.http.get(this.apiUrl).subscribe(data => {
// resolve(data);
// this.storage.set('user', data);
// }, err => {
// console.log(err);
// });
// });
// }
//--------------------------------------------
getUsers() {
// let data = new URLSearchParams();
// data.append('user', 'khaled');
// data.append('pass', 'khaled');
//data =Object.assign(urlSearchParams, {search: "person"});
const data = new HttpParams()
.set('user', 'khaled').set('pass', 'khaled');
this.http
.post(this.apiUrl, data, {headers:{'Content-Type': 'application/json'}})
.subscribe(data => {
alert('ok');
}, error => {
console.log(error.json());
});
}
}
And the Java part
@POST
@Path("/crunchifyService")
@Produces("application/json")
@Consumes("application/json")
public Modele add (String user, String pass) throws ClassNotFoundException, SQLException{
Auth_Ctrl auth = new Auth_Ctrl();
int result =auth.connect(user, pass);
return auth.info(result);
}
the problem is that when I debug the server part all my parameters are null and there is no error in the client part
dataa JSON object?HttpParamsin your angular code.URLSearchParamsbelongs to the deprecatedHttpModule.