Missing parameters on http.post request

Hello everyone!

First of all this is my first month with ionic, i have readed a lot of post but i cant get out this problem.
Im making a login with the jwt authentication method and the get options for sending the key and retrieve the JWT is working well, but i have a problem during the login process when i need to retrieve all the data associated with the given email.

I think my headers are ok.
I don’t understand how to pass the data in the body with the correct method.

@ionic/cli-utils : 1.19.2
ionic (Ionic CLI) : 3.20.0

There is my code:

login(cred) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization', 'Key ABCjhdlkewroirrt9845lkdf');
    return new Promise(resolve => {
      this.http.get(this.link + "/auth/register", {
          headers: headers
        })
        .map(res => res.json())
        .subscribe(
          data => {
            resolve(data)
            localStorage.setItem('bearer', (data.jwt)); //where i store the jwt 
            console.log("auth", data);
          },
          error => {
            this.events.publish('app:toast', JSON.parse(error._body).message);
          }
        )
    })
  }

  validate(data){

      return new Promise(resolve => {
      let bearer = (localStorage.getItem('bearer')); 
      let headers = new Headers({   
        'content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'authorization': 'Bearer ' + bearer
    });

      var body = data; //(where i store the email) ; 

/**
the data structure is {email: "jsmith@aol.com"} 
**/

    var options = new RequestOptions({ headers: headers });
     return this.http.post(this.link + "/getguest" , body, options) 
    

        .subscribe(response => {
          if (response) {
            resolve(response);
          } else {
            resolve(false);
            
          }
        }, error => {

          resolve(false);
          this.events.publish('app:toast', "Error while trying to load data");
        }
      );

With the login() im going to make the call to retrieve the jwt, then i store it in the localStorage e use in the validate() function where with the jwt and the email value im going to ask the server to search the user data associated with the email.

But when i make the http.post call the server return me Missing Parameters. The body data is already an object and i dont need to converto to a json.

Sorry if the english is not perfect .
I hope somehow can help me
Thanks in advance

If you don’t get any better answers, I would start by making the following housecleaning modifications:

  • all method arguments, return values, and properties get actual types: no omissions, no any
  • no Promises at all
  • use HttpClient instead of old Http
  • first word of any function that interacts with HttpClient should be return
  • no using storage of any kind for in-app communication, only storing values across app restarts
  • no attempting to explicitly set Content-Type
  • always use let for lexically scoped variables, never type the word var
  • never subscribe to Observables in the same function they are created in

Thanks rapropos for the answer, i’ve cleaned all my code with the best practice but im starting to thinking if is a problem related to the php server wich send the reponse to the call.
During an accurate analysis i’ve verified the data sended in the body sìis correct, all the parameters are ok but i always receive a missing parameters message.
The json data is in the format {“email”:“jsmith@aol.com”} .
Tomorrow i’ll try to understand if the server is configured to accept json data , but with POSTMAN i’ve seen if i post a json or a form data i always receive a good response with all the data .
If anyone has other idea im happy to try…
I’ll keep the post updated if anyone have the same problem

Best regards

After some research th eproblem was that the server doesn’t expect json data and return me that error.
thanks for the help

Best regards

Riccardo