3

I'm trying to upload a file and some key value pairs to a server.

The IT guy of the server tells me "When our server is receiving the request the file is showing up great, but none of the data in your key value pairs are showing up in the request (as it’s parsed by ASP.Net). I haven’t determined exactly what’s going on with that yet, but I suspect it’s getting buried by being in the dataContent object one layer deeper in the content object."

The code I'm using is below. How can I move the key value pairs "up a level"? I'm not all that savvy with networking, unfortunately.

using (var content = new MultipartFormDataContent())
{
    var dataContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("Key1", "Value1"),
        new KeyValuePair<string, string>("Key2", "Value2")
    });
    content.Add(dataContent);

    using (var fileStream = File.OpenRead(filePathAndName))
    using (var fileStreamContent = new StreamContent(fileStream))
    {
        fileStreamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
        {
            Name = "File",
            FileName = Path.GetFileName(filePathAndName)
        };
        fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        content.Add(fileStreamContent);
        using (var response = await client.PostAsync("api/datasets", content))
        {
            if (response.IsSuccessStatusCode)
            {
                Debug.WriteLine("Upload Success");
            }
            else
            {
                Debug.WriteLine("Upload Failed");
                Debug.WriteLine("Response : " + response);
            }
        }
    }
}

Instead of using a FormUrlEncodedContent, I also tried

content.Add(new StringContent("Value1"), "\"Key1\"");
content.Add(new StringContent("Value2"), "\"Key2\"");

but no luck

jo phul
  • 559
  • 1
  • 6
  • 25
  • Your content.Add(new StringContent("value", "key1") (without additional quotes in the key) should be ok. You can find an example e.g. here: https://stackoverflow.com/questions/19954287/how-to-upload-file-to-server-with-http-post-multipart-form-data. – Christoph Lütjen Oct 26 '18 at 20:53
  • I removed the extra quotes, and still no luck – jo phul Oct 28 '18 at 15:41
  • Could you please provide some more details including how you current code looks like and what you get as response (status code, response body, exceptions?) – Christoph Lütjen Oct 28 '18 at 18:40

1 Answers1

0

Turns out the issue was server side

jo phul
  • 559
  • 1
  • 6
  • 25