4
votes

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.

2
Just making sure, the response coming from target server starts with \n ? - Aviram Segal
Yes. The very first line is just \n and second line starts with "[". - user2133963

2 Answers

3
votes

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)

4
votes

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.