web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Hannes Holst Profile Picture
    5,767 on at

    HI,

    What's the reason you are working with XMLhttp and not System.Net.HttpWebRequest ?

  • jgaztanaga Profile Picture
    225 on at

    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...

  • Hannes Holst Profile Picture
    5,767 on at

    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.

  • Hannes Holst Profile Picture
    5,767 on at

    Hm,

    but you have the payload in fiddler.

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

  • jgaztanaga Profile Picture
    225 on at

    I get this:

    pipe.png

    Is this correct?

  • Hannes Holst Profile Picture
    5,767 on at

    Does it look the same when you send using Postman?

  • jgaztanaga Profile Picture
    225 on at

    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

  • jgaztanaga Profile Picture
    225 on at

    The headers also look different.

    From NAV:

    2804.postman.png

    From postman:

    2804.postman.png

  • I Gusti Made Ari Profile Picture
    3,600 on at

    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

    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....

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans