question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No content is written in PDFs uploaded using the instructions for multipart/form-data

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
canton7commented, May 22, 2020

(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!

1reaction
sdg-sjbcommented, May 22, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multipart/form-data request -- Uploading pdf is resulting in ...
I have an objective to send a pdf file from one server to a REST API which handles some archiving. I am using...
Read more >
ContentVersion data uploaded using Multipart / Form ...
It looks like the issue is with the way the binary data is being encoded in the multipart form data.
Read more >
CF11 enctype="multipart/form-data" uploadind a file
Solved: Hi, I am using Cold Fusion 11. When I browse to obtain the file to upload, the location and file in the...
Read more >
Question: How to upload a file using HTTP POST Form-Data?
I want to upload a file using HTTP POST where username and password are to be filled using form-data. It works well from...
Read more >
HTTP POST files with multipart/form-data
We are getting internal server error in HTTP POST request using Content type as “multipart/form-data” for uploading local PDF file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found