1
votes

I'm trying to retrieve data from an HTTP adapter using the new REST API.
this is some of my JSON objects returned:

    "items": [
        {
            "category": "category 1",
            "produit": [
                {
                    "id": "57",
                    "name": "this is my name",
                    "answer": [
                        {
                            "id": "146",
                            "answername": " answer 1",
                            "istrue": "0",
                            "questionid": "57"
                        },
                        {
                            "id": "147",
                            "answername": "answer 2",
                            "istrue": "0",
                            "questionid": "57"
                        }
                   ]
               }
          ]
      }
 ]

When I invoke the procedure using WL.Client.invokeProcedure(invocationData, options); it works fine.

           var invocationData = {
                    adapter : 'AuthentificationAdapter',
                    procedure : 'getquestion',
                    parameters : [jsontab],
                };
            WL.Client.invokeProcedure(invocationData,{
                onSuccess : $.proxy(function(data)
                {
                    deferred.resolve(data.invocationResult.items);
                },this),
                onFailure : $.proxy(function(error)
                {
                    deferred.reject(error);
                },this)
            });
            return deferred.promise

But when I used the REST API it returns with Failed to read the HTTP response and Failed to parse JSON string

here is my resource request code:

var resourceRequest = new WLResourceRequest("/adapters/AuthentificationAdapter/getquestion", WLResourceRequest.POST, 30000);
            resourceRequest.setQueryParameters(jsontab);
            resourceRequest.send().then(
                $.proxy(function(data) {
                    deferred.resolve(data.responseJSON.items);
                },this),

                $.proxy(function(error) {
                    deferred.reject(error);
                },this)
            );

            return deferred.promise;

It seems that the REST API doesn't support complete JSON object as a return like WL.Client do?

1
it belongs to the old way RPC . are you sure that REST API support results like JSON tree ?Simao

1 Answers

2
votes

WL.Client.invokeProcedure returns a promise so you should use the following (for the first part) instead of implementing your own.

var invocationData = {
         adapter : 'AuthentificationAdapter',
         procedure : 'getquestion',
         parameters : [jsontab],
};

return WL.Client.invokeProcedure(invocationData);

WLResourceRequest.send also returns a promise so you should use

var resourceRequest = new WLResourceRequest("/adapters/AuthentificationAdapter/getquestion", WLResourceRequest.GET, 30000);
resourceRequest.setQueryParameter('params', [jsontab]);
return resourceRequest.send();

Notice that you have to use setQueryParameter and as the first argument you have to pass 'params' and as the second an array containing all the arguments for the adapter function.

FYI: I'm assuming that the previous two snippets of code are inside a function and that's why I'm returning a promise just like you did before.