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', 'http://eshop.XXX-eu.com/ws/api/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:

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:
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
I’ve run the filder to monitor my request from postman, and I see this:

I’ve changed the code to this, using codeunit 1297, and still the same problem:
lHttpWebRequestMgt.Initialize('http://eshop.XXX-eu.com/ws/api/rest');
lHttpWebRequestMgt.DisableUI;
lHttpWebRequestMgt.SetMethod('POST');
lHttpWebRequestMgt.SetContentType('application/json');
lHttpWebRequestMgt.SetReturnType('application/json');
lHttpWebRequestMgt.AddBodyAsText(pJson);
lHttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders);
HttpWebRequest := HttpWebRequest.Create( UseURL);
HttpWebRequest.Timeout := 30000;
HttpWebRequest.Method := FORMAT(WSCWebService.“JSON-Rest HttpVerb”); // GET, POST, PUT …
HttpWebRequest.ContentType := ‘application/json’;
HttpWebRequest.Headers.Add( ‘Authorization’,
APIAppID.TokenType+’ '+AccessToken);
HttpWebRequest.Headers.Add( ‘apikey’, APIAppID.“API-Key”);
SetRequestStream( HttpWebRequest, JSONBody);
IF NOT GetJsonRestResponse( WSCWebService.“Context ID”, HttpWebRequest,ResultSet,JSONresultString) THEN
BEGIN
ErrorText := GetWebResponseError( WebException, UseURL, JSONresultString, ResultSet);
…
where SetRequestStream looks like:
**SetRequestStream(**VAR HttpWebRequest : DotNet “System.Net.HttpWebRequest”;VAR String : DotNet “System.String”)
// var StreamWriter: dot net System.IO.StreamWriter.‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’
StreamWriter := StreamWriter.StreamWriter(HttpWebRequest.GetRequestStream);
StreamWriter.Write(String);
StreamWriter.Close;
StreamWriter.Dispose;
Hi,
Thanks for your answer. I’ve find something similar, but I’m not sure how to use it. What about the “headers.add” sentences? What are APIAppId, token… values? And API-Key? I don’t have nothing similar to that…
Also, I don’t need to get a response, just to send the json to the service
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!!!