0

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

3
  • is data a JSON object? Commented Feb 20, 2018 at 10:48
  • no, it's not a JSON object how can i convert it to a JSON object Commented Feb 20, 2018 at 10:58
  • I agree with the answer provided by Chris, but if you are indeed using query params, you need to use HttpParams in your angular code. URLSearchParams belongs to the deprecated HttpModule. Commented Feb 20, 2018 at 11:31

2 Answers 2

1

You are using @QueryParam which uses get parameters. These parameters should be included in the url. The http.post method encodes the data as application/json inside the body of the request.

You should change either one of these to match the other. My advise would be to change the java here to consume applications/json due to the security concerns of putting a password in the url

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your help, so i have edited my post with my new code i'm using now HttpParams and added @Consumes("application/json") before my methode but the problem now is that i get user=khaled&pass=khaled as my first param and nothing as my seconde param
0

For whom it might interest i modified my code like that i added a class "Log" which contains two fields user and pass in my webservice following thislink and then i modified my method like that

@POST
	@Path("/crunchifyService")
	@Produces("application/json")
	@Consumes("application/json")
	public Modele auth (Log user) throws ClassNotFoundException, SQLException{
			
		Auth_Ctrl auth = new Auth_Ctrl();
		int result =auth.connect(user.user, user.pass);
		
		return auth.info(result);
	}
and i noticed that i need to add jackson-mapper-asl to my classpath and after that everything works fine

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.