Restsharp POST /files/v3/files Bad Request with no Content

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.

Hello @JPerna
AddHeader(“content-type”, “multipart/form-data”); header. This header is not necessary for the /files/v3/files endpoint. In fact, if you include this header, you will get a “Bad Request” response. remove the AddHeader(“content-type”, “multipart/form-data”); header from your code. Once you have done this, your request should be successful.

POST https://api.hubapi.com/files/v3/files
Authorization: Bearer your_access_token
Content-Type: application/json

{
 "file": {
 "fileName": "my-file.pdf",
 "fileBytes": "your_file_bytes"
 }
}

I am starting work on our v3 API integration using Contacts. I’ve tried both sample code on HS dev docs, Endpoints Contacts C# example code, as well as code generated after successful Postman v3 Contacts API call, using restSharp, and then tried C# HTTP Request. I’m on .NET 4.7 Webforms. Visual Studio 2017, the system will not allow synchronous methods. But I’m in a control, .ascx, not at page level so I cannot add Asynchronous=“True” to the page directive. Any advice is appreciated.

I’m in the same boat as you. You may have already figured this one out, but yes.

Adding Asynchronous=“True” to your page directive will allow for async, task, await, etc. code.:hugs: