Problem to Read Json Stream REST API

Martin_Förster
Member Posts: 55
Hi Guys,
I'm trying to read a REST API Stream (Json) but something doesn't work.
For example:
i try the same Configuration with Postman and get a json stream, with ca. 2250 KB (Only Body Size)
like this:

IF i use the same Configuration in NAV, and read the Respons, ist look like this.

The Blob Size is exactly the Same (2250 KB).
My Code is this:

I'm trying to read a REST API Stream (Json) but something doesn't work.
For example:
i try the same Configuration with Postman and get a json stream, with ca. 2250 KB (Only Body Size)
like this:

IF i use the same Configuration in NAV, and read the Respons, ist look like this.

The Blob Size is exactly the Same (2250 KB).
My Code is this:

0
Best Answer
-
I don't know about how HttpWebResponse works in detail, but your response looks like binary data rather than text, which you should get, if the server indeed sends you a plain text json document.
Please inspect the response headers to see, if you got what you expected. Also, the response might be compressed (and a response header should tell you if this is the case, and also what compression algorithm the server used. If the response is compressed, you should be able to tell the server to not use compression (using a request header), or you may just decompress the response before consuming it.1
Answers
-
I assume the HttpStatusCode = 200 in Postman. Please double check in C/AL code if this is also the case. Pseudo code:
IF GLHttpStatusCode.ToString = GLHttpStatusCode.OK.ToString THEN MESSAGE(myjson)
Also try to store the ResponseStream in Tempblob:
TempBlob.Blob.CREATEINSTREAM(Response);
and then read as following:
myjson := TempBlob.ReadAsText('',TEXTENCODING::UTF8)0 -
Thx for your answer
the httpstatus is OK, this works
After ".ReadAsText" i Display the message and it Shows nothing
the tempblob Looks like this
0 -
Hello @Martin_Förster,
Looks like the blob have data, maybe the problem is the READTEXT, try to use READ and WHILE NOT JsonInStream.EOS and see what you got.
Regards.0 -
It Seems the Encoding is the Problem.
I Change the Code to
WHILE JsonInStream.READ(NewContent) > 0 DO
FileContent += NewContent;
and get this Content after reading.
maybe the DotNet component is wrong, is it possible that i shoud work with "httpClient" Component?1 -
HttpWebResponse := HttpWebRequest.GetResponse; IF NOT HttpWebResponse.StatusCode.Equals( HttpStatuscode.InternalServerError) THEN BEGIN StatusCodeInt := HttpWebResponse.StatusCode; StatusResultStr := STRSUBSTNO(' "httpstatus": { "code": "%1", "description": "%2" }}', StatusCodeInt, HttpWebResponse.StatusDescription); StreamReader := StreamReader.StreamReader(HttpWebResponse.GetResponseStream); JSONresultString := StreamReader.ReadToEnd; END;
with these dotnet variables:
HttpWebResponse DotNet System.Net.HttpWebResponse.'System, Version=4.0.0.0
HttpStatuscode DotNet System.Net.HttpStatusCode.'System, Version=4.0.0.0
StreamReader DotNet System.IO.StreamReader.'mscorlib, Version=4.0.0.0
JSONresultString DonNet System.String.'mscorlib, Version=4.0.0.0
The value in the dotnet string can be stored in a blob using this:TempBlob.WriteAsText( JSONresultString, TEXTENCODING::UTF8); MessageLog."Response Message BLOB" := TempBlob.Blob;
2 -
I don't know about how HttpWebResponse works in detail, but your response looks like binary data rather than text, which you should get, if the server indeed sends you a plain text json document.
Please inspect the response headers to see, if you got what you expected. Also, the response might be compressed (and a response header should tell you if this is the case, and also what compression algorithm the server used. If the response is compressed, you should be able to tell the server to not use compression (using a request header), or you may just decompress the response before consuming it.1 -
Maybe this also helps.
You could insert a proxy between Nav and the API to inspect the headers in detail and compare with a working scenario.0 -
I Solved the Problem
The Webserver compressed any Response, automaticlly.
The Solution was this Code Line:
HttpWebRequestMgt.SetDecompresionMethod(DecompressionMethod.GZip);
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions