Reputation: 11
I have a problem in in MobileFirst Foundation 8.0 where the parameter in a POST request is not captured in the adapter, but in a GET request the parameter is working fine.
Example for GET request
var url = "/adapters/samplePOST/unprotected/";
var resourceRequest = new WLResourceRequest(url, WLResourceRequest.GET);
resourceRequest.setQueryParameter("params","['value1','value2']");
resourceRequest.send().then(function(e){console.log(e)}, function(e){console.log(e)});
Adapter side
function unprotected(user){
return {
result:JSON.stringify(user)
};
}
Output
{\"result\":\"\"sd\"\",\"isSuccessful\":true}
Example for POST request
client side
var url = "/adapters/samplePOST/unprotected/";
var resourceRequest = new WLResourceRequest(url, WLResourceRequest.POST);
var data={"params": "['sd','ds']"};
resourceRequest.sendFormParameters(data);
resourceRequest.send().then(function(e){console.log(e)}, function(e){console.log(e)});
Adapter side
function unprotected(user){
return {
result:JSON.stringify(user)
};
}
Output
{\"isSuccessful\":true}
In POST request adapter side, the parameter is undefined
.
Also, in my development console -> swagger, if I hit POST Request form query [{"params": "['sd','ds']"}]
its working fine but from a cordova application the parameter can't be parsed but in same cordova application GET Request working fine.
Upvotes: 1
Views: 1014
Reputation: 700
the problem is you mis-used the sendFormParameters() API . use the (asynchronous promise) JavaScript API correctly. in your examples use either sendFormParamters() or send() but not both API. these two functions are different ways to achieve same goal.
For example:
var resourceRequest = new WLResourceRequest(url, WLResourceRequest.POST);
var data={"params": "['value1', 'value2']"};
var result = resourceRequest.sendFormParameters(data).then(
function(response){
alert ("responseText: " + response.responseText + " ** responseJSON: " + JSON.stringify(response.responseJSON));
},
function(err){
alert (JSON.stringify(err));
}
);
the later API seems to work with no issues.
A PMR is being opened for the send() API. when using MFP v8.0 Resource request with POST and empty body : content type must be set to application/x-www-form-urlencoded if combined with the send() API. The Android code will fail (iOS works fine). simple workaround: when writing the WLResourceRequest manually add an HTTP "Content-Type" header like this:
var resourceRequest = new WLResourceRequest(url, WLResourceRequest.POST);
resourceRequest.setQueryParameter("params", "['value1', 'value2']");
resourceRequest.setHeader('Content-Type','application/x-www-form-urlencoded');
resourceRequest.send().then....more code here...
Upvotes: 2
Reputation: 44516
Update: note that you should change how you're making the request to the following:
resourceRequest.sendFormParameters(data).then(
function(e) {
console.log(e)
},
function(e){
console.log(e)
}
);
sendFormParameters
actually sends the request, so no need to also use send
. Otherwise, you're sending the parameter - twice, which triggers this issue.
I've tried it myself and it is strange.
In Swagger I indeed get the following as the response body for a POST request as you've demonstrated:
{
"result": "\"value1\"",
"isSuccessful": true
}
But in the application the result
property is missing from the response object.
Printing the param
value in the adapter using MFP.Logger.info
I can see the following two logger lines in the application server's messages.log file, even though I had only a single logger line:
[10/9/16 10:57:00:643 IDT] 00000ec7 MFP.Logger I ********** the param value is: sd
[10/9/16 10:57:00:646 IDT] 00000f3e MFP.Logger I ********** the param value is: undefined
One with the value and another without... I suppose the undefined one is returned to the client, which is why you get no result value(?).
Upvotes: 2