web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

File uploading from D365FO to third party portal

(0) ShareShare
ReportReport
Posted on by 2
Hi Expert, 
 
I have a query, I need to upload .txt file from D365 FO to a third party portal, by giving required information in postman and a custom Dot net application I can successfully uploaded .txt file, but when it comes to D365 FO side, it gives me error / The remote server returned an error: (415) Unsupported Media Type/,  the parameters in postman are mentioned in a screenshot 
1. Postman Screenshot :
2: Dot Net Code: 
 static void UploadFile(string accessToken)
        {
            var client = new RestClient(https:/api-superstream.sandpit.ozedi.com.au/api//);
            var request = new RestRequest(/uploads/process/3511577632/, Method.Post);
            // Add Authorization header with token
            request.AddHeader(/Authorization/, /Bearer / + accessToken);
            
            // Add other parameters if needed
            request.AddParameter(/abn/, /67094544519/);
            request.AddParameter(/account/, /629992780050304/);
            // Read file as bytes or stream
            byte[] fileBytes = File.ReadAllBytes(@/C://Users//munsif//Downloads//TestGhzanfar.txt/);
            // Add file as parameter
            request.AddFile(/payloadFile/, @/C://Users//munsif//Downloads//TestGhzanfar.txt/, /multipart/form-data/);
           
            var response = client.Execute(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine(/File upload successful/);
            }
            else
            {
                Console.WriteLine(/Error uploading file: / + response.Content);
            }
        }
    }
    public class TokenResponse
    {
        public string id_token { get; set; }
        // You may include other properties from the token response if needed
    }
3. D365 FO code :
 
 System.Byte[]                buffer;
         System.Byte[]                fileBuffer;
         str                           boundary = /--myboundary//r//;
         str                           boundryConntentType = /content-type: charset=UTF-8//r//;
         str                           contentType = /multipart/form-data; boundary = myboundary/;
         str                           fileName = /Ghzanfar-test.txt/;
         str                           fileContentType =/application/octet-stream/; ///application/json/;
         str                           postData;
         System.IO.Stream             fileStream;
        
        str paramName = /abn/;
        str paramValue = /67094544519/;
        str paramName1 = /account/;
        str paramValue1 = /629992780050304/;

        // Construct the POST data with file and parameters
        postData += boundary;
        postData += /Content-Disposition: form-data; name=///abn/////r///r//;
        postData += paramValue + ///r//;
        postData += boundryConntentType;
        postData += boundary;
        postData += /Content-Disposition: form-data; name=///account/////r///r//;
        postData += paramValue1 + ///r//;
       // postData += boundryConntentType;
        postData += boundary;
        postData += /Content-Disposition: form-data; name=///payloadFile///; filename=//// + fileName + //////r//;
        postData += /Content-Type: / + fileContentType + ///r///r//;
         
        // Open the file
        fileStream = stream;
        fileBuffer = new System.Byte[fileStream.get_Length()]();
        fileStream.Read(fileBuffer, 0, fileStream.get_Length());
        fileStream.Close();
        // Convert the file contents to a base64 string
        str fileContentBase64 = System.Convert::ToBase64String(fileBuffer);
        // Add file contents to the POST data
        postData += fileContentBase64 + ///r//;
        postData += boundary + /--//r//;
            
        // Convert the request body to bytes
        System.Byte[] byteArray = System.Text.Encoding::UTF8.GetBytes(postData);
            
        // Set the content length
        request.set_ContentLength(byteArray.get_Length());
            
        // Get the request stream and write the bytes
        dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.get_Length());
        dataStream.Close();
         try
        {
            response = request.GetResponse();
            dataStream = response.GetResponseStream();
            streamRead = new System.IO.StreamReader(dataStream);
            responseContract = FormJsonSerializer::deserializeObject(classNum(DHRP_SuperStreamUploadResponseContract), streamRead.ReadToEnd());
            dataStream.Close();
            response.Close();
        }
        catch
        {
            exception = CLRInterop::getLastException();
            System.Net.WebException webEx = exception.InnerException;
            if (webEx.Status == System.Net.WebExceptionStatus::ProtocolError)
            {
                var errorResponse = webEx.Response as System.Net.HttpWebResponse;
                if (errorResponse && errorResponse.StatusCode == 400)
                {
                    str resp = new System.IO.StreamReader(errorResponse.GetResponseStream()).ReadToEnd();
                    DHRP_SuperStreamErrorContract error = FormJsonSerializer::deserializeObject(classNum(DHRP_SuperStreamErrorContract), resp);
                    throw error(error.parmDetail());
                }
            }
            throw error(webEx.Message);
        }
        codeAccessPermission::revertAssert();
        return responseContract;
    }
can you please suggest me how to fix the issue? or what needs to refactor in the code at D365 FO side?
 
I have the same question (0)
  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at
    I wonder why your C# code is completely different from your X++ code. Shouldn't you use the same approach in X++ too, or write C# that tests what you actually want to do?

    (Moved from Dynamics AX forum.)
  • Rachit Profile Picture
    4,015 User Group Leader on at
    What data are you trying to get from D365 F&O? Is there any standard data entity which exposes that data? Have you also evaluated using Electronic Reporting Framework to generate the .txt file from F&O and push it out to a supported destination and then your .net application can just pick the file and upload it to third party? 
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 646 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans