Upload File by consuming REST web service

shanabeywicrema
Member Posts: 53
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
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
0
Answers
-
I need to do the same. Send 2 parameters and a text file (in the body of Request) to a WS Rest using the POST method.
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?0 -
Hello,
The REST API that you want to call, what was expecting, IMHO that it is the first thing that you need to figure out.
Regards0 -
shanabeywicrema...
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);
0 -
Now I have the problem that I do not know how to add to the body three parameters together, 2 string + the file. Since if I invoke first the function .AddBodyAsText and then .AddBody gives error:
"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'..".0 -
Thank you for the post. It gave me a head start. I found the solution (using Business Central Spring Release) to sent a multpart/form-data.
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.1 -
https://www.kauffmann.nl/2015/11/26/web-services-examples-part-1-the-basic-pattern/
Take help from this!!Thanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/0 -
RockWithNAV wrote: »https://www.kauffmann.nl/2015/11/26/web-services-examples-part-1-the-basic-pattern/
Take help from this!!
Dear RockWithNAV what is the added value of this blog for this problem?0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions