Search code examples
iosangularlaravelcordovaionic-framework

Data sent out from Cordova advanced-http post as json is not received by Laravel backend properly


I'm trying to send some data to my Laravel backend from my Ionic/Cordova/Angular frontend

import { HTTP } from '@ionic-native/http/ngx';
...
constructor(private http: HTTP){    
}

public postMyData(){
    return new Promise((resolve, reject) => {
        let data = { 'name': 'My name', 'age' : '42'}
        let url = this.baseURL + '/api/profile';
        let headers = {
            "Authorization": "Bearer " + access_token,
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        this.http.setDataSerializer('json');
        this.http.post(url, profile, headers).then(data => {
            resolve(data.data);
        }).catch(error => {
            reject(error);
        });
    }
}

What I receive on my Laravel endpoint as $request is

{"{\"name\":\"My name\",\"age\":\"42\"}":null}

Any idea why is the data in bold appended, and who does this alteration?

The request sent out from Postman works perfectly and it is not escaped at all!


Solution

  • Soon after I posted, I found the solution:

    I had to change the serialization from

    this.http.setDataSerializer('json');
    

    to

    this.http.setDataSerializer('urlencoded');
    

    I was distracted by my json data so I ignored this one.