4

I am using IBM Worklight Studio, and trying to create HTTP Adapter which retrieve JSON object from external http server.

When I just access target http server with HTTP Get access(with browser, for example), I know their response is like following array style JSON format:

[
  { "xxx":"aaa", "yyy":"bbb", ... },
  { "xxx":"ccc", "yyy":"ddd", ... },
    :
  { "xxx":"eee", "yyy":"fff", ... } 
]

And I had created HTTP Adapter which would retrieve above information

var input = {
  method : 'get',
  returnedContentType : 'json',
  path : path
};

return WL.Server.invokeHttp(input);

Now I tried to invoke this adapter with "Run As -> Invoke Worklight Procedure", then I got this error message:

{
   "errors": [
      "Runtime: Failed to parse JSON string\n\n[\n  {\n
   (raw JSON data) } ],
   "info": [],
   "isSuccessful": false,
   "warnings": []
}

And in my log console, worklight says following error messages:

FWLSE0101E: Caused by: java.io.IOException: Expecting '{' on line 2, column 2 instead, obtained token: 'Token: ['

From above information, it seems that worklight would expect that returned JSON object need to start with "{", not "[".

Is this my guess right? Are there any workaround for this?

Thanks for advance.

user2133963
  • 95
  • 2
  • 7

2 Answers2

4

Worklight knows how to handle JSON objects that start with [ (JSON arrays). In such case Worklight will return the response as:

{ "array" : [*the json array*]}

Looking at the code of the HTTP Adapter, I see that there is a bug with parsing JSON arrays that do not start with [.

I do not see a workaround for this problem, except changing the response returned from the http server.

I opened an internal bug about this, thank you for helping us find this bug.

  • I appreciate your support. I have checked again, and it seems response from http server starts with `\n`, so your guess would be correct. But is that wrong format? Actually this http server is other IBM product(Lotus Domino), and I don't think this format would be able to be customized. – user2133963 Mar 10 '13 at 23:27
  • Sure, I understand. I appreciate for looking into this problem. – user2133963 Mar 11 '13 at 08:09
  • 1
    If you are an IBM customer you might want to open a PMR about this issue, a solution might be given there. –  Mar 11 '13 at 15:02
3

You can change returnedContentType to "plain", this will make WL server return content as a big string and not attempt to parse it. Then in your adapter you can use var obj = JSON.parse(response.text)

Anton
  • 3,166
  • 1
  • 13
  • 12
  • Thanks Anton. I have changed my HTTP Adapter's .js file to get plain text format as you suggested( returnedContentType : 'plain' ), which leads WL.Client.invokeProcedure succseeded. I have also changed my success callback function's javascript as "var objs = JSON.parse( result.invocationResult.text );". I have confirmed this works as I expected. Many Thanks!! – user2133963 Mar 15 '13 at 01:04