When uploading data files with RestSharp to a .Net Core 2.1 REST interface, I get this error on some files:
System.IO.InvalidDataException: Line length limit 100 exceeded.
at Microsoft.AspNetCore.WebUtilities.BufferedReadStream.ReadLineAsync(Int32 lengthLimit, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.MultipartReader.ReadNextSectionAsync(CancellationToken cancellationToken)
When I do the same operation with Insomnia, I do not get any error, only with RestSharp.
I have read here https://stackoverflow.com/questions/44642701/microsoft-aspnetcore-webutilities-line-length-limit-100-exceeded that there is a problem with the line ending formats, but this is the only article about it.
The controller signature is very simple:
[HttpPost, DisableRequestSizeLimit]
public async Task<IActionResult> AddPictureAsync(
IFormFile Picture,
IFormFile Thumbnail,
string Title)
{
and the code to send is:
var Request = new RestRequest("picture", Method.POST) { AlwaysMultipartFormData = true };
Request.AddHeader("SessionId", SessionId);
Request.AddFile("Picture", PhotoData.ToArray(), Photo.PhotoUrl);
Request.AddFile("Thumbnail", ThumbnailData, Photo.PhotoUrl.Replace(".jpg", "-t.jpg"));
Request.AddParameter("Title", Photo.Title ?? string.Empty, ParameterType.GetOrPost);
I have replaced RestSharp by HttpClient; same call, same data, and it works; so the issue is definitely in RestSharp.
It looks like it has to do with the formatting of the data passed. I run it on Mac, so possibly the issue raised in the link I provided, regarding CRLF expected at the end of the data, could be the problem.
I also wrote to the .NET Core project and they pointed out to the same problem.
Maybe RestSharp does the encoding of the end of lines differently on the Mac.
Well, the answer on SO is clear. We also use Environment.LineBreak
but of course this comes from the client environment and server can have a different environment, so it breaks. It should be solved in the next version.
I'd like to point you gents to https://tools.ietf.org/html/rfc2616. The sequence of characters separating header and body has to be CRLFCRLF, so \r\n\r\n, regardless of what system you're sending from or to. Anything else and every standardconforming webserver (nginx, haproxy, apache among others) will fail to understand the request properly.
Is there an ETA for this? right now I've to move code away from RestSharp in order to have the calls succeed.
It is fixed a long time ago.
Most helpful comment
Is there an ETA for this? right now I've to move code away from RestSharp in order to have the calls succeed.