Upload File by consuming REST web service

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

  • guiyoteguiyote Member Posts: 12
    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?
  • ftorneroftornero Member Posts: 522
    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.

    Regards
  • guiyoteguiyote Member Posts: 12
    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);
  • guiyoteguiyote Member Posts: 12
    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'..".
  • SjoerddeClippelaarSjoerddeClippelaar Member Posts: 2
    edited 2019-08-08
    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#. .
    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.
  • SjoerddeClippelaarSjoerddeClippelaar Member Posts: 2

    Dear RockWithNAV what is the added value of this blog for this problem?
Sign In or Register to comment.