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
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?