Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Rest Request with CSV File attachment in Navision(CAL)

Posted on by 70

Hi All,

I would like to seek your help to make a rest request with CSV file in the body part

Rest request sample:

pastedimage1628235234697v2.png

so I referred GetResponseText function from the standard codeunit Web Request Helper and build the code logic as below

I am sure this method is working for normal request but not with CSV attachment.

Please share your views on below code

HttpWebRequest := HttpWebRequest.Create(Url);
HttpWebRequest.Method := 'POST';
//HttpWebRequest.ContentLength := 0; //0 means without body, plain request
HttpWebRequest.ContentType := 'multipart/form-data;//from data for sending file attachment

HttpWebRequest.Headers.Add('Authorization','Token ' + AccessToken);// add the access token to the authorization bearer header
FileManagement.IsAllowedPath(BodyFilePath,FALSE);
//Add body file as stream
FileStream := FileStream.FileStream(BodyFilePath,FileMode.Open);
FileStream.CopyTo(HttpWebRequest.GetRequestStream);


HttpWebResponse := HttpWebRequest.GetResponse; //Calling URL with stream added in body

TempBlob.INIT;
TempBlob.Blob.CREATEINSTREAM(ResponseInputStream);
HttpWebResponse.GetResponseStream.CopyTo(ResponseInputStream);

// the READTEXT() function apparently only reads a single line, so we must loop through the stream to get the contents of every line.
WHILE NOT ResponseInputStream.EOS DO BEGIN
ResponseInputStream.READTEXT(ChunkText);
ResponseText += ChunkText;
END;


MESSAGE('%1',ResponseText); //display response

Appreciated your support and suggestions..!

Thank you in advance

  • Sathish G Profile Picture
    Sathish G 70 on at
    RE: Rest Request with CSV File attachment in Navision(CAL)

    Hi Suresh,

    Thank you for your support.

    I tried in postman 

    The link you can find below which I mapped in postman along with the authorization key (Token 123456789) token is just an example::reality token we are getting from the concerned team.

    below is the header part of the REST request

    pastedimage1629098777796v1.png

    in the body section, I selected form-data & given form in the key so that I selected file instead of text

    pastedimage1629099036554v2.png

    imported the file in the value section and clicked on the post button

    As a result, we are getting file names as confirmation in JSON format.

    Now, as I explained in my first post, I tried to add the file as a stream after referring to standard codeunit  1297(Http Web Request Mgt.)

  • Suggested answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Rest Request with CSV File attachment in Navision(CAL)

    Are you trying to send the whole contents of the file as text of the request body? what error message you are getting when you send this way? have you tried using postman to send the file in this way ?

  • Sathish G Profile Picture
    Sathish G 70 on at
    RE: Rest Request with CSV File attachment in Navision(CAL)

    Hi John,

    Thank you for the advice.

    I could not able to use data exchange reason is that we have a unique requirement for 3rd party integrations

    Example: Person A is giving CSV files from his system and copied to our Navision server, here I am importing CSV file data to one table and making some verification, once data is verified then I send the same CSV file to Person B

    Person A: We get only CSV files from him

    Navision: We are auditing the data

    Person B: We need to send the same CSV file via REST

    The solution we tried:

    We tried to send the REST  request, it is working. we lacking knowledge about how to send a REST request with a CSV file in REST Body

    Same we are able to simulate in postman selecting *form-data* for the body, there we have flexibility to add the file directly

    Action: we did research on REST with CSV file attachment in body, we found no working solution sample code I have shared in my first post

    asked many seniors but no one came across s this kind of requirement.

    I really appreciate it if you share any idea on this

  • Suggested answer
    JAngle Profile Picture
    JAngle 33,159 on at
    RE: Rest Request with CSV File attachment in Navision(CAL)

    Could you use a data exchange definition to layout the structure of the csv file and also map it to the required table(s). Would mean potentially storing the file as a blob before it is passed through the exchange definition.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans