Getting 400 Bad request

I’m trying to upload a file, but I keep getting 400 bad request, code below:

var options = new {
access = “PUBLIC_INDEXABLE”,
ttl = “P3M”,
overwrite = false,
duplicateValidationStrategy = “NONE”,
duplicateValidationScope = “ENTIRE_PORTAL”
};
var url = “/filemanager/api/v3/files/upload”;
var properties = JsonConvert.SerializeObject(options);
var fileInfo = new FileInfo(“C:\\Users\\Pedro\\Desktop\\beagle.jpg”);
using var content = new ByteArrayContent(File.ReadAllBytes(fileInfo.FullName));
var postContent = new MultipartFormDataContent();
postContent.Add(content, “file”, “beagle.jpg”);
postContent.Add(new StringContent(properties), “options”);
postContent.Headers.ContentType = new MediaTypeHeaderValue(“multipart/form-data”);

var result = _httpClient.PostAsync(url, postContent).Result;

Hey, @PGraça :waving_hand: Thank you for your question. Finding C# specific examples in the community can indeed be a bit challenging.

Please bear with my limited knowledge here. I wonder if it is because of the way the ‘options’ object is being handled in your request. Instead of serializing the ‘options’ object as a single JSON string, you should consider adding each field in the ‘options’ object as its own separate field in the form data. That is, ‘access’, ‘ttl’, ‘overwrite’, ‘duplicateValidationStrategy’, and ‘duplicateValidationScope’ should each be their own key-value pair in the form data.

Please let us know if this works. If not, I’ll look for some community members who might have relevant experience.

Best,

Jaycee