Rest api(POST Method) in ionic 2 not working on android device

We are building an application in ionic 2 using API, api calls working fine on browser(used proxy Server) .

After android build(removed proxy server) GET method is working fine on device but POST method is not working. It is an API for user authentication so getting 403 forbidden error.

Below is the code:- Home.ts

this.authService.checkAuthentication().then((res) =>{
  console.log("Already authorized");
 this.data = res;
}, (err) => {
  this.data = 'error occured';
});

Auth.ts(Auth Provider)

checkAuthentication() {
    return new Promise((resolve, reject) => {
      let credentials = {
        email: 'test@test.com',
        password: 'password'
      };
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
        headers.append('X-XSRF-Token', 'kK4GDdiHAHejPeoXl4dL1AAgb13eiEwPhxvDw=');
      let options = new RequestOptions({ headers: headers });
      this.http.post(API_URL, JSON.stringify(credentials), options)
        .map(res => res.json())
        .subscribe((data) => {
          resolve(data);
        }, err => {
          reject(err);
        });
    });
  }

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.

Did you remote debug the problem on the device already? Follow these instructions here to debug the problem in Chrome dev tools: https://ionic.zone/debug/remote-debug-your-app#android Look at the console and network tabs for errors.

Potential CORS issue, maybe?

having the same issue here. I don’t see how it could be CORS since the GET works perfectly fine.

Hi
You problem is in the backend, not in ionic.
Remember that the POST values that send Angular are received in payload json format. Some backend frameworks require some customization to manage payload format.

Hi Alex, I was suspicious of our backend but not able to find the issue. what’s curious though is that iOS is going for all request types. It’s just Android blocking at 403 on POST. We have Cloud Front for content but that’s probably OK because images come through. we use keycloak for security and I’m wondering if that’s my issue? maybe Android doesn’t use localhost or /127.0.0.1/ ?

Hi @saywhut
did you checked if the “email” and “password” are received correctly in the backend?