bassco_dp
bassco_dp

Reputation: 29

Add parameters in JS HTTP Adapters for MobileFirst 8

I am trying to use JavaScript HTTP Adapters in order to get some data from a Cloudant BD in Bluemix. For that, I am using MFPF8 and Ionic2 with TypeScript.

Since I need to get a specific document from the Database of which I do not previously know its name, I need to have an HTTP Adapter that lets me send the filename as a parameter.

I have the following adapter implementation file:

function getMenus() {
    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'menus/_all_docs?descending=true'
    };

    return MFP.Server.invokeHttp(input);
}

function getSpecificMenu(menuName) {

    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'menus/'+menuName
    };

    return MFP.Server.invokeHTTP(input);

}

And here is the adapter.xml

<mfp:adapter name="menus"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:mfp="http://www.ibm.com/mfp/integration"
             xmlns:http="http://www.ibm.com/mfp/integration/http">

    <displayName>menus</displayName>
    <description>menus</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>https</protocol>
            <domain>bluemixcloudanthost.com</domain>
            <port>443</port>
            <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds>
            <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds>
            <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode>

            <authentication>
                <basic/>
                <serverIdentity>
                    <username>user</username>
                    <password>pass</password>
                </serverIdentity>
            </authentication>

        </connectionPolicy>
    </connectivity>

    <procedure name="getMenus" secured="false"/>
    <procedure name="getSpecificMenu" secured="false"/>

</mfp:adapter>

So, following the API documentation, I did the following to call the adapter within my ionic provider

@Injectable()
export class MenuListingService {
  data: any;

  constructor() {
    console.log('---> Constructing menu list adapter');
    this.data = null;
  }

  load(menuTitle: string) {
    console.log('---> Request '+menuTitle+' Menu');
    if (this.data) {
      // already loaded data
      return Promise.resolve(this.data);
    }

    // don't have the data yet
    return new Promise(resolve => {

     let menuRequest = new WLResourceRequest("/adapters/menus/getSpecificMenu", WLResourceRequest.GET);
     menuRequest.setQueryParameter('menuName', menuTitle);

     menuRequest.send().then((response) => {
          console.log('---> Current menu response received');
          this.data = response.responseJSON.offers;
          resolve(this.data);
      })

    });
  }
}

Reading in the Knowledge center, I found something about calling the paremeters with a querystring, like ?params=['value'], but it is failing with a 500 code.

Please, keep in mind that I am not at the office, so I will be adding an edit tomorrow to give you all more info, with the server response and the MFP server log entries.

But, as for now, is there anything wrong with what I've done?

Upvotes: 0

Views: 803

Answers (1)

Idan Adar
Idan Adar

Reputation: 44526

See here: https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/using-the-mfpf-sdk/resource-request/javascript/#setqueryparameter

More specifically:

JavaScript adapters use ordered nameless parameters. To pass parameters to a Javascript adapter, set an array of parameters with the name params: resourceRequest.setQueryParameter("params", "['value1', 'value2']");

What you're missing is the use of "params" instead of menuRequest.setQueryParameter('menuName', menuTitle);.

Upvotes: 1

Related Questions