Problem to Read Json Stream REST API

Martin_FörsterMartin_Förster Member Posts: 55
edited 2021-03-22 in NAV Three Tier
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:

rsezesxnf7kg.jpg

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

ll03tdta6c0u.jpg

The Blob Size is exactly the Same (2250 KB).

My Code is this:

dfvroxb495sk.jpg



Best Answer

  • vaprogvaprog Member Posts: 1,140
    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.

Answers

  • irasoelbaksirasoelbaks Member Posts: 119
    edited 2021-03-22
    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)
  • Martin_FörsterMartin_Förster Member Posts: 55
    Thx for your answer

    the httpstatus is OK, this works

    After ".ReadAsText" i Display the message and it Shows nothing

    xkuj2mgedtf5.jpg

    the tempblob Looks like this

    megdo306xg1b.jpg

  • ftorneroftornero Member Posts: 524
    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.
  • Martin_FörsterMartin_Förster Member Posts: 55
    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.

    ybhcpls445t2.png

    maybe the DotNet component is wrong, is it possible that i shoud work with "httpClient" Component?
  • Remco_ReinkingRemco_Reinking Member Posts: 74
    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;
    
  • vaprogvaprog Member Posts: 1,140
    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.
  • irasoelbaksirasoelbaks Member Posts: 119
    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.
  • Martin_FörsterMartin_Förster Member Posts: 55
    I Solved the Problem

    The Webserver compressed any Response, automaticlly.

    The Solution was this Code Line:

    HttpWebRequestMgt.SetDecompresionMethod(DecompressionMethod.GZip);


Sign In or Register to comment.