MobileFirst Platform JavaScript Adapter Cannot Receive Parameters via WLResourceRequest

I am using mobilefirst v7 framework and I am sending post request using WLResourceRequest / sendFormParameters api, however I cannot get the presented parameters from the js adapter side ...

belows - example code:

var resourceRequest = new WLResourceRequest("adapters/businessAdapter/flightsearch", WLResourceRequest.POST);
var params={
        "flightNum":'mu8899',
        "departCity":'SHA',
        "destCity" :'PEK'
};
resourceRequest.sendFormParameters(params).then(
        callSuccess,
        callFailure
);

      

Js adapter code:

function flightsearch(params) {
   WL.Logger.info("get params "+params);


    var input = {
        method : 'post',
        returnedContentType : 'json',
        path : 'restapi/api/flightsearch',
        body :{
            contentType: 'application/json; charset=utf-8',
            content:params
        },
        headers: {"Accept":"application\/json"} 
    };

    return WL.Server.invokeHttp(input);
}

      

+3


source to share


1 answer


The syntax you used works great for Java adapters.

However, in the case of JavaScript adapters, procedure parameters are handled differently.

First, your adapter routine must define the expected parameters:

function flightsearch(flightNum, departCity, destCity) {
///
}

      

Secondly, this procedure will be run using HTTP GET

or POST

with a single named parameter params

, which must contain an array representing all the procedure parameters in the correct order:



params:["mu8899","SHA","PEK"]

      

Now using JavaScript this will translate to:

var resourceRequest = new WLResourceRequest("adapters/businessAdapter/flightsearch", WLResourceRequest.POST);
var params=[
        'mu8899',
        'SHA',
        'PEK'
];
var newParams = {'params' : JSON.stringify(params)};
resourceRequest.sendFormParameters(newParams).then(
        callSuccess,
        callFailure
);

      

As you can see, we will first construct the JSON array (note the array is not an object) in the correct order, then we convert it to String and send it to the adapter with the parameter name ' params

.

+5


source







All Articles