0

Can someone explain what I'm doing wrong here? I'm specifying the length header. No idea why its saying "Length Required". This request works fine when I send it through Postman, but fails when I send it through Flurl in C#. Heres the API docs for the call.

Error uploading attachment: Flurl.Http.FlurlHttpException: POST https://xxx.leankit.com/kanban/api/card/SaveAttachment/365784226/733054060 failed with status code 411 (Length Required).\r\nResponse body: Bad Request \nYour browser sent a request that this server could not understand.

Upload File code

public static async Task<string> UploadFile(string externalcardid, string filePath,string filename,int boardId,long size)
{
    try
    {
        var url = "https://xxx.leankit.com/kanban/api/card/SaveAttachment/" + boardId + "/" + externalcardid.ToString();
        var responseString = await url.WithBasicAuth("xxx", "yyy")
            .WithHeader("Content-Length", size)
            .WithHeader("Content-Type", "multipart/form-data")
            .PostMultipartAsync(mp=> mp
                .AddFile(filename,filePath)
            )
      .ReceiveString();
        return (responseString);
    }
    catch (Exception e)
    {
        return ("Error uploading attachment: " + e);
    }
}

enter image description here

Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • First thing to check - are you on the most current version of Flurl.Http? This could be related to [this bug](https://github.com/tmenier/Flurl/issues/256), which was fixed last spring in 2.3.1. Just want to rule it out. – Todd Menier Sep 18 '18 at 13:57
  • @ToddMenier I updated my Flurl version and now I get: `Flurl.Http.FlurlHttpException: Call failed. Error while copying content to a stream. POST https://xxx.leankit.com/kanban/api/card/SaveAttachment/365784226/733054060 ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The write operation failed, see inner exception. ---> System.Net.Http.WinHttpException: The parameter is incorrect` – Rilcon42 Sep 18 '18 at 16:53
  • How are you calculating content-length? My understanding is that it's [a bit complicated](https://stackoverflow.com/questions/31406022/how-is-an-http-multipart-content-length-header-value-calculated) with multipart. And are you certain that the same value is being sent in that header with both Postman and your code? – Todd Menier Sep 18 '18 at 17:40
  • @ToddMenier Im using file.Size which returns the size in bytes from http://www.dotnet-stuff.com/tutorials/aspnet-core/understanding-file-uploads-in-aspnet-core which after reading your comment seems incorrect. I dont suppose Flurl has some magic to do it for me? Thanks for all the help so far! – Rilcon42 Sep 18 '18 at 18:10
  • No such magic I'm afraid. Are you setting that header _explicitly_ in Postman or is Postman doing it for you? If it's setting it for you, check what value is sent and make sure it matches what's sent in your code. Also note that now your code is failing before it even attempts to send the request. If you remove _just_ that header from your code and you're back to getting a 411 from the server, you'll know that that header is the problem. – Todd Menier Sep 18 '18 at 18:58

0 Answers0