HTTPRequest from NAV C/AL - added BLOB is not recognised by file-storage URL

1k views Asked by At

I'm trying to send a PDF file generated by a NAV Report (MS Dynamics NAV 2018) via httprequest made in C/AL. Response is a JSON containing an UUID, which I need to read for later purpose. No matter what I try when building the request, the storage does not find my attached file and returns "ERROR:MISSING_FILE". Unfortunately, I'm not too good with neither the DotNet Automations nor the Webrequests, so it's been a lot of trial and error for me up to this point.

This is my code:

variables

SalesShipmentHeader Record  Sales Shipment Header
    
locFile File
        
locFile2    File    
    
fileName    Text        200
    
txt Text
        
ResponseHeaders DotNet  System.Collections.Specialized.NameValueCollection.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    
HttpStatusCode  DotNet  System.Net.HttpStatusCode.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    
HttpWebRequestMgt   Codeunit    Http Web Request Mgt.
    
FileManagement  Codeunit    File Management
    
TempBlob    Record  TempBlob
    
TempJSONBuffer  Record  JSON Buffer

ResponseJsonText    Text
        
NumRead Integer 
    
InStr   InStream
        
OutStr  OutStream       

Code

// make temp file and remember path
locFile.CREATETEMPFILE;  
fileName := locFile.NAME;
locFile.CLOSE;

SalesShipmentHeader.SETFILTER("No.",DocNo);
IF SalesShipmentHeader.FINDFIRST THEN BEGIN
  locReport.SETTABLEVIEW(SalesShipmentHeader);
  IF locReport.SAVEASPDF(fileName) THEN BEGIN
    // check file and build request
    IF NOT locFile2.OPEN(fileName) THEN
      ERROR('File could not be opened');

    CLEAR(HttpWebRequestMgt);
    HttpWebRequestMgt.Initialize(url);
    HttpWebRequestMgt.DisableUI;
    HttpWebRequestMgt.SetMethod('POST');
    HttpWebRequestMgt.SetContentType('multipart/form-data; boundary=--123456789');
    HttpWebRequestMgt.SetContentLength(locFile2.LEN);   
    HttpWebRequestMgt.SetUserAgent('Dynamics NAV 2018 Client');
    HttpWebRequestMgt.SetReturnType('application/json');
    HttpWebRequestMgt.AddSecurityProtocolTls12; 
    HttpWebRequestMgt.AddHeader('Accept-Encoding','gzip, deflate, br');

    FileManagement.BLOBImportFromServerFile(TempBlob,fileName);
    HttpWebRequestMgt.AddBodyBlob(TempBlob); 
    
    locFile2.CLOSE();    
    
    CLEAR(TempBlob);
    
    HttpWebRequestMgt.CreateInstream(InStr);  //Initialize
    
    IF NOT HttpWebRequestMgt.GetResponse(InStr, HttpStatusCode, ResponseHeaders) THEN
      ERROR(GETLASTERRORTEXT);

    //CheckStatusCode
    //...

    //process JSON
    CLEAR(ResponseJsonText);
    WHILE NOT InStr.EOS() DO BEGIN
      NumRead := InStr.READTEXT(txt, 100);
      IF NumRead > 0 THEN
        ResponseJsonText := ResponseJsonText + txt;
    END;

    MESSAGE(ResponseJsonText);
    
    // DELETE FILE AFTER UPLOAD
    ERASE(fileName);
    EXIT(TRUE)
  END ELSE
    EXIT(FALSE);
END;


EXIT(FALSE)

I can successfully make the request via Postman App: Postman Screenshot 1 Postman Screenshot 2

What am I missing out?

1

There are 1 answers

0
mateo On

You are trying to send empty file - that can be the reason. Try to send a file with the content.