HTTP POST Response Not Detected


#1

Hello everyone ^.^

So far I am enjoying Nativescript and this is my first post on this forum :slight_smile:

My app is making a POST request to my server and sending some login data. It gets to the server OK and the server responds with a status 200, but for some reason the server response is not being picked up by the app. I am using code similar to the POST example provided here: https://docs.nativescript.org/ns-framework-modules/http

httpModule.request({

        url: "https://myTestServer.com/sign-in",
        method: "POST",
        headers: { "Content-Type": "application/json" },
        content: JSON.stringify({
            username: page.getViewById("username").text,
            password: hash
        })

    }).then(function(response){

        const result = response.content.toJSON();
        console.log("response: " + result);
        

    }, function(e){
        console.log("error: " + e.content.toJSON());
    });

This issue happens on both iOS and Android. I have tried deliberately mistyping the URL and hoped that I would get an error message, but that didn’t work either.

Getting images from my server works when I make a GET request in the XML file, so I’m not sure what’s gone wrong here.

I greatly appreciate any help!!
Nat


#2

Did you verify the server response using any REST client, may be POSTMAN?

If it works on REST client but not only in app, would you mind setting up a playground example where we can reproduce the issue?


#3

Hello ^.^

Thanks for the reply!

I used Postman and the server definitely works. I managed to solve my problem by using “fetch” instead of “http”, although I’m still a little curious why original code doesn’t work.

Nat


#4

I can’t see what your server is running but using PHP on the server I had to do this:

$_POST = json_decode(file_get_contents('php://input'), true);