Hi Everyone,
Can anyone share good example for upload file / send file by consuming external REST Web service ?
My end point would be something like bellow
https://myapi.com/v1/documents/upload
I have to pass three parameters, API Key, Workflowid and file.
I wrote bellow code,
userFile := 'D:\Temp\Helvetia.pdf';
RequestURL := '
https://myapi.com/v1/documents/upload';
Req := '{"api_key":"5445989G98398fs3t5","workflow_id":"939925","userfile":"'+userFile+'"}';
Request.open(HTTP_POST_REQUEST,RequestURL,FALSE,'','');
Request.setRequestHeader('Content-Type','multipart/form-data');
Request.send(Req );
IF Request.status = 200 THEN
MESSAGE(Request.responseText);
But I am getting error. But I try using SOAPUI, it worked.
Can anyone suggest good method to pass file by consuming external REST API
Thanks
Answers
I am testing with the codeunit 1297 and with family dotnet variables (System.Net.Http.HttpClient.'System.Net.Http)
Can someone tell us how to do, please?
The REST API that you want to call, what was expecting, IMHO that it is the first thing that you need to figure out.
Regards
If you use the codeunit 1297, you can invoke the AddBody function that, as a parameter, requires the path of the file to be sent.
example:
HttpWebRequestMgt=cu1297
myPath:='D:\Temp\Helvetia.pdf';
RestUrl :='https://myapi.com/v1/documents/upload';
HttpWebRequestMgt.Initialize(RestUrl);
HttpWebRequestMgt.SetMethod('POST');
HttpWebRequestMgt.SetReturnType('*/*'); //Accept: */*
HttpWebRequestMgt.SetContentType('multipart/form-data');
HttpWebRequestMgt.AddBody(myPath);
IF HttpWebRequestMgt.GetResponse(Instr,HttpStatusCode,ResponseHeaders) THEN
MESSAGE(RegistrationOK);
ELSE MESSAGE(RegistrationError);
"Error en la llamada a System.IO.FileStream.CopyTo con el mensaje siguiente: No se puede obtener acceso a una secuencia cerrada.
Nombre del objeto: 'destination'..".
First of all I would like you to refer you to these links to see how it's done in C#.
- https://www.paraesthesia.com/archive/2009/12/16/posting-multipartform-data-using-.net-webrequest.aspx/
- https://stackoverflow.com/a/2996904
.As you can read there it's also possible to add other fields than only a file.
I created the following function.
---
LOCAL UploadRequest(ActionDesc : Text;Method : Text;URLRequestPath : Text;Base64string : Text;FileName : Text;VAR ResponseJson : Text;VAR HttpError : Text) : Boolean
//Header
HttpWebRequestMgt.Initialize('https://api.com/api/'+URLRequestPath);
HttpWebRequestMgt.SetMethod(Method);
boundary := '-(27x)' + FORMAT(DateTimeManagement.DT2BI(TODAY,TIME));
HttpWebRequestMgt.SetContentType('multipart/form-data; boundary=' + boundary);
HttpWebRequestMgt.SetReturnType('application/json');
HttpWebRequestMgt.AddHeader('Authorization', CreateJWT);
//Content
TempBlob.FromBase64String(Base64string);
TempBlob.Blob.CREATEINSTREAM(InStr);
CLEAR(TempBlob);
TempBlob.Blob.CREATEOUTSTREAM(OutStr);
OutStr.WRITETEXT(TypeHelper.NewLine +
'--' + boundary + TypeHelper.NewLine +
STRSUBSTNO('Content-Disposition: form-data; name="file"; filename="%1"', FileName) + TypeHelper.NewLine +
'Content-Type: application/pdf' + TypeHelper.NewLine +
TypeHelper.NewLine);
COPYSTREAM(OutStr, InStr);
OutStr.WRITETEXT(TypeHelper.NewLine + '--' + boundary + '--');
HttpWebRequestMgt.AddBodyBlob(TempBlob);
HttpWebRequestMgt.DisableUI;
EXIT(HttpWebRequestMgt.GetResponseJson(ResponseJson))
---
In the ResponseJson the node Content has all the information if the request is succesful.
Take help from this!!
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
Dear RockWithNAV what is the added value of this blog for this problem?