Search code examples
c#restsharp

RestSharp addfile sends file with ".undefined" file extension


I'm uploading PDF file to node.js server with code below:

var file = new FileContentResult(...)
var client = new RestClient(_tenantOptions.BaseUrl + _tenantOptions.UploadPdfFilesUrn) {Timeout = _tenantOptions.RequestTimeoutMilliseconds};
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", accessToken);
request.AddFile("file", file.FileContents, file.ContentType);
var response = client.Execute<UploadFileResponse>(request);

This file appears on server like filename.undefined though file.ContentType = "application/pdf". What did I miss?


Solution

  • According to the source code for RestSharp there's a 4-parameter form of AddFile that accepts the filename too.

    Try

    request.AddFile("file", file.FileContents, "document.pdf", file.ContentType);