I'm trying to use .net core 3, to invoke the Box REST API, to upload a small file.
The .net core code is based on the information provided on: Upload a file.
I haven't been successful so far, and all my attempts have resulted in a 400: Bad Request error.
This is code snippet:
var client = _clientFactory.CreateClient("box-upload");
var dto = new UploadFileContent() {
attributes = new UploadFileContentAttributes() {
name = fileDto.file.FileName,
parent = new UploadFileContentParent() {
id = fileDto.folderId
}
}
};
var content = new MultipartFormDataContent();
content.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
var jsonContent = new StringContent(JsonConvert.SerializeObject(dto), Encoding.UTF8, "application/json");
jsonContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
content.Add(jsonContent, "attributes");
if (!string.IsNullOrWhiteSpace(token))
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BearerToken, token);
var streamContent = new StreamContent(fileDto.file.OpenReadStream());
content.Add(streamContent, "file");
var response = await client.PostAsync(url, content);
What am i missing?
Thank you in advance.