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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Custom Connector #Code...
Power Automate
Unanswered

Custom Connector #Code multipart/form-data

(0) ShareShare
ReportReport
Posted on by 174

Hey all,

 

I am struggling with a multipart/form-data within my custom connecter. I want to achieve to transform the body at the code Section. But I am not able to pass the date related to this error. Maybe somebody got a hint for me.. 

Failed to provision compute for custom code. Request failed with error 'Code: BadRequest. Message: script.csx(96,49): error CS1503: Argument 1: cannot convert from 'System.Net.Http.MultipartFormDataContent' to 'System.Net.Http.HttpRequestMessage'.'. The correlation Id is '80848aa7-f551-41e7-a98e-bb3a27eb5386'.

 

 

public class Script : ScriptBase
{
public override async Task<HttpResponseMessage> ExecuteAsync()
{
 // Check if the operation ID matches what is specified in the OpenAPI definition of the connector
 Context.Logger.LogTrace("Script started");
 if (this.Context.OperationId == "UploadFile_Lexoffice")
 {
 //Context.Logger.LogInformation($"Operation [{Context.OperationId}] {Context.Request.Method} {Context.Request.RequestUri} was called.");
 return await this.UploadFile().ConfigureAwait(false);
 }

 // Handle an invalid operation ID
 Context.Logger.LogError($"Unknown operation ID '{this.Context.OperationId}'");
 HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
 response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'");
 return response;
}

private async Task<HttpResponseMessage> UploadFile()
{
 Context.Logger.LogTrace("AdjustBodytoMultiForm");
 // get requestContent and parse object
 var requestContentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
 var requestContentAsJson = JObject.Parse(requestContentAsString);

 // Set
 const string quote = "\"";
 //receive Value from request
 var fileType = (string)requestContentAsJson["fileType"];
 var fileTitle = (string)requestContentAsJson["fileTitle"];
 var contentType = (string)requestContentAsJson["contentType"];
 var content = (string)requestContentAsJson["content"];

 //set fileObjects
 var fileContent = new JObject()
 {
 {"$content-type" , contentType},
 {"$content" , content}
 };

 //multiform Binary file record
 var fileContentBytes = Encoding.UTF8.GetBytes(fileContent.ToString());
 var binaryFile = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent.ToString()),0,fileContentBytes.Length);
 binaryFile.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
 binaryFile.Headers.ContentDisposition.Name = "\"file\"";
 binaryFile.Headers.ContentDisposition.FileName = quote + fileTitle + quote;

 //Set FileType record
 var fileTypeRecord = new StringContent(fileType);
 fileTypeRecord.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
 fileTypeRecord.Headers.ContentDisposition.Name = "\"type\"";
 fileTypeRecord.Headers.ContentType = null;

 // Set MultiFormBody
 MultipartFormDataContent multiFormBody = new MultipartFormDataContent();
 multiFormBody.Add(fileTypeRecord);
 multiFormBody.Add(binaryFile);

 this.Context.Request.Content = multiFormBody;
 //Context.Logger.LogTrace("multiformString "+ await this.Context.Request.Content.ReadAsStringAsync());
 Context.Logger.LogTrace("contextRequest "+ this.Context.Request.ToString());

 // Make API request
 var response = await this.Context.SendAsync(multiFormBody, this.CancellationToken).ConfigureAwait(continueOnCapturedContext: false);

 // Update response
 var responseContentAsString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
 var responseContentAsJson = JObject.Parse(responseContentAsString);
 response.Content = CreateJsonContent(responseContentAsJson.ToString());
 Context.Logger.LogTrace("responseBody " + response.ToString());

 return response;
}
}

 

 

 

 

 

 

Categories:

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 355

#2
11manish Profile Picture

11manish 336 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 308

Last 30 days Overall leaderboard