Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Problem sending JSON to a REST api

(0) ShareShare
ReportReport
Posted on by 225

Hi everyone,

I'm finding an issue sending a json to an API.

The sending function is this:

SendJSONtoAPI(VAR pJson : Text) : Text
IF ISCLEAR(XMLHTTP) THEN
  CREATE(XMLHTTP,FALSE,TRUE);
XMLHTTP.open('POST', 'eshop.xxx-eu.com/.../rest');
XMLHTTP.setRequestHeader('Content-Type: ', 'multipart/form-data');
XMLHTTP.setRequestHeader('Host','eshop.XXX-eu.com');
XMLHTTP.send(pJson);


In fidler, as far as I know, seems to be a correct call:

0lw2fmt5ck8c.png
wl9363537mbb.png


But the adminsitrator of the service, is telling me that he is receiving an empty cal...

So I tried to use postman as he has recommend me, like this:

yd527d32lgqn.png


And it works correctly. I think that may be I'm asending incorrectly the content of the JSON. In postmna, the content is assigned to a variable called "json". Could be that the problem? How can I reproduce that in the NAV call?

Thaank you very much

*This post is locked for comments

  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    Seems that the problem is solved, using this code:

     HttpWebRequest := HttpWebRequest.Create('eshop.xxxx-eu.com/.../rest');
     HttpWebRequest.Method := 'POST';
     HttpWebRequest.ContentType('application/x-www-form-urlencoded');
     postString := STRSUBSTNO('json=%1',pJson);
     StreamWriter := StreamWriter.StreamWriter(HttpWebRequest.GetRequestStream);
      StreamWriter.Write(postString);
      StreamWriter.Close;
      StreamWriter.Dispose;
      HttpWebRequest.GetResponse;
     


    Thanks for all the tips, really appreciated!!!

  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    THnaks for your answer.

    I've coded like this:

     HttpWebRequest := HttpWebRequest.Create('ennwc7wnqy24r.x.pipedream.net');
     HttpWebRequest.Method := 'POST';
     HttpWebRequest.ContentType('application/x-www-form-urlencoded');
     postString := STRSUBSTNO('json=%1',pJson);
     HttpWebRequest.ContentLength := STRLEN(postString);
     StreamWriter := StreamWriter.StreamWriter(HttpWebRequest.GetRequestStream);
      StreamWriter.Write(postString);
      StreamWriter.Close;


    And I get the error "Error in the call to System.IO.StreamWriter.Close with the message: Cannot close the sequence until all the bites are written".

    And in requestBin, I see this:

    4452.error.png

    I've quited "HttpWebRequest.ContentLength := STRLEN(postString);" sentence, and I don't get the error. But like this, I don't receive any request into requestBin....

  • I Gusti Made Ari Profile Picture
    3,594 on at
    RE: Problem sending JSON to a REST api

    Did you tried with 'application/x-www-form-urlencoded' for the content type ?

    Request.ContentType('application/x-www-form-urlencoded');
    postString := STRSUBSTNO('json=%1',YourJSONRequestString);
    Request.ContentLength := STRLEN(postString); 
    StreamWriter := StreamWriter.StreamWriter(Request.GetRequestStream);
    StreamWriter.Write(postString);
    StreamWriter.Close;


  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    The headers also look different.

    From NAV:

    2804.postman.png

    From postman:

    2804.postman.png

  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    Hi again,

    It looks different.

    Executing from the postman, I can see this:

    2019_2D00_05_2D00_02-09_5F00_01_5F00_51_2D00_Window.png

    As you can see, the content is loaded into a variable called "json", created in the postman this way:

    2019_2D00_05_2D00_02-09_5F00_01_5F00_51_2D00_Window.png

    As I expected, this seems to be the problem, the service is waiting to receive the content in a variable called "json". How can I simulate this?

    Thank you very much

  • Hannes Holst Profile Picture
    5,767 on at
    RE: Problem sending JSON to a REST api

    Does it look the same when you send using Postman?

  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    I get this:

    pipe.png

    Is this correct?

  • Hannes Holst Profile Picture
    5,767 on at
    RE: Problem sending JSON to a REST api

    Hm,

    but you have the payload in fiddler.

    Did you try to send your request to a service like https://requestbin.com/ ?

  • Hannes Holst Profile Picture
    5,767 on at
    RE: Problem sending JSON to a REST api

    Hi,

    I'm using a function like this to send a body:

    LOCAL SetRequestStream(VAR HttpWebRequest : DotNet "System.Net.HttpWebRequest";VAR String : DotNet "System.String")
    StreamWriter := StreamWriter.StreamWriter(HttpWebRequest.GetRequestStream);
    StreamWriter.Write(String);
    StreamWriter.Close;
    Name DataType Subtype
    StreamWriter
    DotNet
    System.IO.StreamWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Maybe this helps.

  • jgaztanaga Profile Picture
    225 on at
    RE: Problem sending JSON to a REST api

    There no reason, just because I have copied from another of my functions, and it was working...

    I've also wrote this code:

    //2
      lHttpWebRequestMgt.Initialize('eshop.xxx-eu.com/.../rest');
      lHttpWebRequestMgt.DisableUI;
      lHttpWebRequestMgt.SetMethod('POST');
      lHttpWebRequestMgt.SetContentType('multipart/form-data');
      lHttpWebRequestMgt.SetReturnType('application/json');
      lHttpWebRequestMgt.AddBodyAsText(pJson);
      lHttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders);


    And the result still the same...

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics NAV (Archived)

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans