Hello professionals,
I'm trying to add BLOB image to JSon request send to API.
First of all I'm converting that photo to BigInteger and after that it's converted into base64string.
APIUploadProductImage(ProductID : Text)
CLEAR(ImageBigText);
recItem.SETRANGE(recItem."No.", '10000');
IF recItem.FINDFIRST THEN BEGIN
recItem.CALCFIELDS(Picture);
IF NOT recItem.Picture.HASVALUE THEN
EXIT;
recItem.Picture.CREATEINSTREAM(Istream);
MemoryStream := MemoryStream.MemoryStream();
COPYSTREAM(MemoryStream,Istream);
Bytes := MemoryStream.ToArray();
ImageBigText.ADDTEXT(Convert.ToBase64String(Bytes));
END;
JsonMgt.StartJSon;
JsonMgt.AddToJSon('image',ImageBigText);
JsonMgt.EndJSon;
String := String.Copy(JsonMgt.GetJSon);
WebServiceURL := '
https://XXXXXXXXXXXXXX/api/2.0/products/'+ProductID+'/actions/image_upload/';
HttpWebRequest := HttpWebRequest.Create(WebServiceURL);
HttpWebRequest.Method := 'POST';
HttpWebRequest.KeepAlive := TRUE;
HttpWebRequest.Timeout := 30000;
HttpWebRequest.Accept('application/json');
HttpWebRequest.ContentType('multipart/form-data');
HttpWebRequest.Headers.Add('Authorization: Bearer ' + 'XXXXXXXXXXXX');
StreamWriter := StreamWriter.StreamWriter(HttpWebRequest.GetRequestStream,Encoding.GetEncoding('iso8859-1'));
StreamWriter.Write(String);
StreamWriter.Close;
NAVWebRequest := NAVWebRequest.NAVWebRequest;
NAVWebRequest.doRequest(HttpWebRequest,HttpWebException,HttpWebResponse);
##########!!!!!!!!!!################!!!!!!!!!!!
and here it breaks at this line.
Error shows:
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at NAVWebRequest.NAVWebRequest.doRequest(HttpWebRequest& WebRequest, WebException& WebException, WebResponse
I'm new with JSon. My brain already stopped working. And I can create/get request/response but in case of adding BLOB to request I have to give up... I think something is wrong with converting blob>biginteger>here?
JsonMgt.Start, add & endJson are functions which are building json schema around data which I'm providing. It works for normal data so it's not problem with it.
Any hints lads? Any idea, tip, all those will be helpful!
Ahh and I'm working on NAV2016.
Thanks in advance!
Tim