Question Dustin Knudsen · Jul 15, 2021

Parsing error

After calling an API with

Do request.Get("/api/address/listing")

Set data = {}.%FromJSON(request.HttpResponse.Data)

 

I get this error: 

<THROW>%FromJSON+37^%Library.DynamicAbstractObject.1 *%Exception.General Premature end of data 12 Line 1 Offset 0

any clue what might be causing this?

Product version: Ensemble 2018.1

Comments

David Hockenbroch · Jul 15, 2021

It looks to me like the data returned in the HTTP response might not be proper JSON. Do you have that data for us to see?

0
Dustin Knudsen  Jul 15, 2021 to David Hockenbroch

If I do 

Set stream=##class(%FileBinaryStream).%New()

Write stream.CopyFrom(getStaffReq.HttpResponse.Data)

I get this large output:

v¯fAPI\-e^zCopyFrom+1^%Library.FileBinaryStream.1^1-e^zGetStaffRequest+21^CompBI.bo.APITest.1^1"d^^%Library.FileBinaryStream.1^0,E^zSizeGet+1^%Library.FileBinaryStream.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0"d^^%Library.FileBinaryStream.1^0-E^zCopyFrom+2^%Library.FileBinaryStream.1^1e^^CompBI.bo.APITest.1^0"d^^%Library.FileBinaryStream.1^0-E^zCopyFrom+2^%Library.FileBinaryStream.1^1e^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0d^^CompBI.bo.APITest.1^0E^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^CompBI.bo.APITest.1^0'd^^%Library.DynamicAbstractObject.1^03E^%FromJSON+37^%Library.DynamicAbstractObject.1^1e^^
CompBI.bo.APITest.1^0d^^^

0
Julius Kavay  Jul 15, 2021 to Dustin Knudsen

This is definitely not a JSON.

0
Robert Cemper  Jul 16, 2021 to Dustin Knudsen

there's a mistake
Methode CopyFrom(source As %AbstractStream) as %Status
just returns an obviously strange status.
to see the content use DO stream.OutputToDevice()

0
Julius Kavay · Jul 15, 2021

Obviously, the response.Data does not contain valid JSON. You can simply check the received data by putting the data aside in a temporary global, something like this:

do request.HttpResponse.Data.Rewind()
set ^temp.debug($j,"size")=request.HttpResponse.Data.Size
set ^("data")=request.HttpResponse.Data.Read(request.HttpResponse.Data.Size)  // or just the first 1000 bytes
zw ^temp.debug

Now you can take a look on the incoming data, maybe there is an encoding problem or the data do not adhere to JSON specification

0
Dustin Knudsen  Jul 16, 2021 to Julius Kavay

Strangely, I recreated that same logic in Powershell using Invoke-Webrequest, and it printed out the json list I'm looking for just fine when I called the API, with a 200 code of success. When I print out the status code in the terminal for the objectscript code, it returns 302, which is a URL redirect, yet they both look as if they have the same functionality.

0
Eduard Lebedyuk  Jul 17, 2021 to Dustin Knudsen

Add:

set request.FollowRedirect = $$$YES

before sending a GET request.

0
Dustin Knudsen  Jul 21, 2021 to Julius Kavay

It returned this:

^temp.debug(6492,"size")=""
^temp.debug(9788,"data")=""
^temp.debug(9788,"size")=0
 

0