Answered by:
[Node.js][Mobile Apps] invokeApi with method POST results in a GET instead

Question
-
I'm porting my Cordova app from Azure Mobile Services to Azure Mobile App. I'm running into numerous problems and one of them is that the invokeApi method on the client seems to always try to issue a GET even when I specify that I need a POST.
Here's the code on the client:
client.invokeApi('dosomething', {
body: { P1: 'valueOfP1', P2: 'valueOfP2' },
method: "POST"
}).done(function (data) {
console.log('success');
}, function (error) {
console.log('failure');
});
This fails with the following message on the js console:
HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
(XHR): GET - https://blabla.azurewebsites.net/api/dosomethingCan you please help?
- Edited by Gary Liu - MSFT Friday, July 1, 2016 6:10 AM edit title
- Moved by Gary Liu - MSFT Friday, July 1, 2016 6:10 AM move thread
Sunday, April 24, 2016 8:42 PM
Answers
-
This issue has been resolved now. The problem was that the MobileServiceClient was being initialized with an http://<application URL>. If authentication is enabled, this redirects to an HTTPS://<application URL> which was causing the reported behavior. To connect to the mobile app backend, simply use an HTTPS URL instead of an HTTP URL.
- Proposed as answer by Swikruti Bose Thursday, May 5, 2016 10:48 AM
- Marked as answer by lucorn Thursday, May 5, 2016 3:11 PM
Wednesday, April 27, 2016 7:06 PM
All replies
-
Hi,
You may double check your script in Easy APIs in Mobile App. Usually, the script's content shows like following (if you need get and post methods under this URI router):
module.exports = { "get": function (req, res, next) { res.send("hits get") }, "post":function(req,res,next){ res.send("hits post") } }
You may check whether there are any syntax error, especially whether you would miss any right braces in some other method functions, which may cause the router cannot find "post" method function.
Also, if it is convenient of you, you can post the script in the Easy APIs for us to diagnose.
Monday, April 25, 2016 2:18 AM -
The code on the backend is intentionally trivial:
module.exports = {
"post": function (req, res, next) {
console.log('POST reached.');
}
}
This seems to be a problem on the client as I specifically want the call to invokeApi to do a POST but instead it tries to issue a GET.
Monday, April 25, 2016 3:49 AM -
Please check your cordova sdk version. Try to use
cordova plugin add cordova-plugin-ms-azure-mobile-apps
to install cordova sdk in cmdlet. Refer to https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-cordova-how-to-use-client-library/ for more info.
This is the version in my test project, and which works fine.
Any further concern, please feel free to let me know.
Monday, April 25, 2016 3:59 AM -
That is the exact version I have in my project.Monday, April 25, 2016 4:09 PM
-
Are you using MobileServiceClient.withFilter() and modifying the request in it? Can you post how you are initializing client and any filters that you might be using?
Which platform are you running your Cordova app on?
Monday, April 25, 2016 7:26 PM -
This issue has been resolved now. The problem was that the MobileServiceClient was being initialized with an http://<application URL>. If authentication is enabled, this redirects to an HTTPS://<application URL> which was causing the reported behavior. To connect to the mobile app backend, simply use an HTTPS URL instead of an HTTP URL.
- Proposed as answer by Swikruti Bose Thursday, May 5, 2016 10:48 AM
- Marked as answer by lucorn Thursday, May 5, 2016 3:11 PM
Wednesday, April 27, 2016 7:06 PM -
Hello.
I was going with the same problem. It works now.
BUT
Doesnot recognize the req.body in the POST
And also the invokeAPI fails...
Yours Pedro
- Edited by ppazpurua Saturday, January 21, 2017 3:04 PM
Saturday, January 21, 2017 3:03 PM