Skip to content

Instantly share code, notes, and snippets.

@cmshawns
Created December 30, 2014 23:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmshawns/49537d4d53d8dd96bed4 to your computer and use it in GitHub Desktop.
Save cmshawns/49537d4d53d8dd96bed4 to your computer and use it in GitHub Desktop.
Upload File to Dropbox with RestSharp
public static void UploadFileToDropbox(string filePath)
{
RestClient client = new RestClient("https://api-content.dropbox.com/1/");
IRestRequest request = new RestRequest("files_put/auto/{path}", Method.PUT);
FileInfo fileInfo = new FileInfo(filePath);
long fileLength = fileInfo.Length;
request.AddHeader("Authorization", "Bearer INSERT_DEVELOPER_TOKEN_HERE");
request.AddHeader("Content-Length", fileLength.ToString());
request.AddUrlSegment("path", string.Format("Public/{0}", fileInfo.Name));
byte[] data = File.ReadAllBytes(filePath);
var body = new Parameter
{
Name = "file",
Value = data,
Type = ParameterType.RequestBody,
};
request.Parameters.Add(body);
IRestResponse response = client.Execute(request);
}
@cmshawns
Copy link
Author

RestSharp's .AddFile() method attaches a file as a multipart form POST, but the current Dropbox API requires that the body of the Request only be the file data, with other information passed in the query string. (And .AddBody() appears to simply call .ToString() when given an object.)

This code uploads a file to a Public folder on your Dropbox account. It's based on the solution to this StackOverflow question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment