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;
}
}

Report
All responses (
Answers (