Hi All,
I would like to seek your help to make a rest request with CSV file in the body part
Rest request sample:
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
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
in the body section, I selected form-data & given form in the key so that I selected file instead of text
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.)
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 ?
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
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156