Ionic frame work sending empty parameters in http post(400 Badrequest)


#1

Method is working fine in postman but, while sending the same request from ionic2 is generating an error.Get methods were working fine but there is some issue in post.The request is sending empty parameters in post and generating error 400 Bad request.

Login.ts

onlogin()
{

let login_email="vvvv@gmail.com";
let login_password="123456"
this.body={
login_email:login_email,
login_password:login_password
};

this.auth.login(this.body).subscribe(data => {
this.result = data

console.log(data);
});

}

authservice.ts

login(param)
{
let headers = new Headers({ ‘Content-Type’: ‘application/json’});
let options = new RequestOptions({ headers: headers });
return this.http.post(this.apiUrl+’/login’,JSON.stringify(param),options)
.map((res: Response) => {
if (res) {
return { status: res.status, json: res.json() }
}
});
}


#2

Please edit your post and use the </> button above the post input field to format your code or error message or wrap it in ``` (“code fences”) manually. This will make sure your text is readable and if it recognizes the programming language it also automatically adds code syntax highlighting. Thanks.


#3

Hi, i think the problem is in your post function, maybe you dont need the requestOptions or JSON.stringify.

Try this one:

login(param) {
    const headers = new Headers({ 'Content-Type': 'application/json'});
    return this.http.post( URL, param, {headers: headers})
              .map((res: Response) => {
                  if (res) {
                      return { status: res.status, json: res.json() }
                  }
   });
}

#4

Hi thanks for the reply,I tried this one but still getting the same issue.
Network error:400 Bad Request
Response:Email or password field is required

Handling the error using catch block generates TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or
Iterable.


#5

Using Formdata and'Content-Type': 'application/form-data resolved my issue

let body = new FormData();
body.append(“login_email”,email);
body.append(“login_password”,password);