I am trying to upload files through the /files/v3/files endpoint. I am using Restsharp in C#. My code is below.
Method for API call:
Spoiler
public T FileUpload<T>(File file)
{
var client = new RestClient(“https://api.hubapi.com/files/v3/files”);
var request = new RestRequest(“”, Method.Post);
request.AlwaysMultipartFormData = true;
request.AddFile(“file”, file.FileBytes, file.fileName, “application/octet-stream”);
request.AddHeader(“authorization”, $“Bearer {_Private_Key}”);
request.AddHeader(“accept”, “application/json”);
request.AddHeader(“content-type”, “multipart/form-data”);
request.AddParameter(“folderId”, file.id);
request.AddParameter(“options”, file.options);
var response = client.Execute<T>(request);
if (response.IsSuccessful)
{
return response.Data;
}
throw new FailedApiCallException(response);
}
File class:
Spoiler
public class File
{
public string id { get; set; }
public string fileName { get; set; }
public string options = “{access:‘PRIVATE’}”;
[JsonIgnore]
public byte[] FileBytes { get; set; }
public File(IFormFile file)
{
byte[] bytes = new byte[file.Length];
file.OpenReadStream().Read(bytes, 0, (int)file.Length);
FileBytes = bytes;
fileName = file.FileName;
}
}
No matter how I format my request I get back a ‘Bad Request’ Response. Unlike most Bad Request Responses, it never has any content/further info.
I have tried many different variations of this code, and this is the only endpoint without code examples that Ive seens on the documentation. I have tried many examples from other posts on this forum but none have worked for me. Even a basic Postman example would be greatly appreciated.
