0

I use RestSharp in version 111.2.0. All our requests are build in this way:

internal async Task<byte[]> GetFileAsync(int documentId)
{
    RestRequest request = new RestRequest($"{_apiPath}/document-files/{documentId}", Method.Get);
    request.AddHeader("Accept", "application/octet-stream");

    RestResponse response = await mRestClient.ExecuteAsync(request).ConfigureAwait(true);
    ErrorHandling(response);

    return response.RawBytes;
}

So, really simple approach. We have a handler written, that makes log files and so on in the ErrorHandling-method. When we donwload a file this way it is "corrupted" by the octet-stream headers. These headers are inside the Content and inside the RawBytes properties.

The file then looks like this:

--69d36367-8ab0-4b7c-a718-668f7542e836
Content-Type: application/octet-stream
Content-Disposition: form-data; name=""; filename="MG Test2.pdf"
 
%PDF-1.7
...
%%EOF
 
--69d36367-8ab0-4b7c-a718-668f7542e836--

But the beginning of the real file is the %PDF-1.7 and %%EOF should be the end of the file. Naturally I could cut it out by hand... but that is not really a good approach, is it? There are coursing some examples in the net for the downloading of files, using the DownloadDataAsync for example. But that does not return a Response but throws errors itself and would require a completely new handling and try catches, etc.

Btw... if I don't add the header by hand I get a 406 error. So it is needed.

Anyone an idea what could be a better way of getting the file than to cut it out myself of the response?

EDIT: Sorry, I found the error and that was on upload. The error only existed on files that I uploaded myself right before. I uploaded them with Request.AddFile(...) instead of Request.AddParameter(...) and that seemed to be the wrong way. I corrected that and now the error is gone.

6
  • 1
    Maybe you should do it as RestClient it does? github.com/restsharp/RestSharp/blob/dev/src/RestSharp/… Commented Jul 1, 2024 at 7:13
  • couldn't really reproduce with default asp.net WebAPI controller from the other end sending a pdf file Commented Jul 1, 2024 at 7:24
  • I don't know what the company used at their side of the API. But I cannot take a look at their servers. :/ Commented Jul 1, 2024 at 7:30
  • @MarcelGrüger maybe use vanilla BCL or Postman to get the whole response...I think the extra --69d36367-8ab0-4b7c-a718-668f7542e836 thrown by the server prevents RestSharp successful parsing Commented Jul 1, 2024 at 9:11
  • 1
    The response is a multipart form, not a plain file content. It looks more like a server issue. Commented Jul 3, 2024 at 16:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.