Search code examples
javascriptnativescripttypo3-8.x

Using HTTP in NativeScript to send Post-data to a TYPO3-Webservice


I'm trying to send form data from a NativeScript app to a TYPO3-Webservice.

This is the JavaScript I'm using:

httpModule.request({
            url: "https://my.domain.tld/webservice?action=login",
            method: "POST",
            headers: { "Content-Type": "application/json" },
            content: JSON.stringify({
                username:username,
                password:password
            })
        }).then((response) => {
             console.log("got response");
             console.log(response.content);
            //result = response.content.toJSON();
            callback(response.content.toJSON());

        }, (e) => {

            console.log("error");
            console.log(e);

        });

But I can't read this data in the controller. Even with this:

$rest_json = file_get_contents("php://input");
$postvars = json_decode($rest_json, true);

$postvars is empty. $_POST is empty, too (which is - according to some docs - because the data is sent as JSON and thus the $_POST-Array isn't populated.

Whatever I do, whatever I try, I can't get those variables into my controller.

I tried it with fetch as well as with formData instead of JSON.stringify, same result.

I might have to add, that when I add the PHP-part in the index.php of TYPO3, $postvars is being populated. So I guess something goes missing, until the controller is called.

Any ideas?


Solution

  • the nativescript part seems ok, your problem must be corrected in the server side.

    i use similare call and its works

    // send POST request
    httpModule.request({
        method: "POST",
        url: appSettings.getString("SERVER") + '/product/list',
        content: JSON.stringify(data),
        headers: {"Content-Type": "application/json"},
        timeout: 5000,
    }).then(response => { // handle replay
        const responseAsJson = response.content.toJSON();
        console.log('dispatchAsync\n\tresponse:', responseAsJson);
    }, reason => {
        console.error(`[ERROR] httpModule, msg: ${reason.message}`);
    });