No content is written in PDFs uploaded using the instructions for multipart/form-data
See original GitHub issueI have an objective to send a PDF file from one server to a REST API which handles some archiving. I am using .NET Core 3.1 and RestEase
I have a simple console app that runs at a certain time everyday. The relevant code is as follows:
using System;
using System.Linq;
using System.Threading.Tasks;
using RestEase;
namespace CandidateUploadFile
{
class Program
{
static async Task Main(string[] args)
{
try
{
var apiClientBuilder = new ApiClientBuilder<ITestApi>();
var api = apiClientBuilder.GetApi("https://localhost:5001");
var candidate = await api.GetCandidateByEmailAddress("tester@aol.com");
var fileName = "tester.pdf";
var fileBytesToUpload = await FileHelper.GetBytesFromFile($@"./{fileName}");
var result = await api.UploadCandidateFileAsync(fileBytesToUpload, candidate.Data.First().Id, fileName);
}
catch (Exception e)
{
System.Console.WriteLine(e);
}
}
}
}
apiClientBuilder
does some auth-header adding, and that’s really it. I’m certain that bit isn’t relevant.
ITestApi
looks like this:
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Models;
using RestEase;
namespace CandidateUploadFile
{
public interface ITestApi : IApi
{
[Get("v1/candidates/{candidateId}")]
Task<Models.Response<Candidate>> GetCandidate([Path] string candidateId);
[Get("v1/candidates")]
Task<Models.Response<IEnumerable<Candidate>>> GetCandidateByEmailAddress([Query] string email);
[Get("v1/candidates")]
Task<Models.Response<IEnumerable<Candidate>>> GetCandidates();
[Post("v1/candidates/{candidateId}/files?perform_as=327d4d21-5cb0-4bc7-95f5-ae43aabc2db7")]
Task<string> UploadFileAsync([Path] string candidateId, [Body] HttpContent content);
[Get("v1/users")]
Task<Models.Response<IEnumerable<User>>> GetUsers();
}
}
Per the docs, I’m using an extension method to handle the multipart/form-data piece, along with headers that are required by my API. That extension method looks like this:
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CandidateUploadFile
{
public static class ApiExtension
{
public static async Task<string> UploadCandidateFileAsync(this ITestApi api, byte[] data, string candidateId, string fileName)
{
var content = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(data);
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "file",
FileName = fileName
};
content.Add(fileContent);
return await api.UploadFileAsync(candidateId, content);
}
}
}
So what will happen when my console app executes is: I will get a successful response from the upload endpoint, and the file on the archive server gets created, but it’s blank.
It may be important to know that this does not happen when I send, say, a .txt file. The .txt file will save with the expected content.
Not sure where to start on this one; it’s not clear as to if this is a library issue or something else. Any insight would be helpful. Thank you!
Issue Analytics
- State:
- Created 3 years ago
- Comments:16 (6 by maintainers)
Top GitHub Comments
(also, reading a file async does nothing unless it was opened as async. You’d also be better off grabbing a FileStream and using that as a SteamContent: then the whole thing doesn’t have to be loaded into memory at once).
Glad you solved it!
I see… That explains why .txt files were working and not pdfs… Yikes, my bad. Foolish of me. Thank you for the help @StefH @canton7