0

I am calling a super old external webservice which has CSV values inside the response:

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <payload-621420A61A932740E10080DB995F3244@XXXXXXX.XXX>
Content-Disposition: form-data;filename="MainDocument.bin";name="MainDocument"
Content-Type: application/json
Content-Description: MainDocument

{"mt_csvfile":"csvfile.csv"}
--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END
Content-ID: <csvfile.csv>
Content-Disposition: form-data;filename="csvfile.csv";name="csvfile.csv"
Content-Type: mimeType text/comma-separated-values;charset="uft-8"
Content-Description: csvfile.csv

"NUMBER","DESCRIPTION",
"00000000001","DESC 1",
"00000000002","DESC 2",
"00000000003","DESC 3",

--XXXXXXX_0d5d4bd4-93a4-11ec-8139-00000611f70a_END--

All I care about is the CSV payload.

How can parse the response? I am not asking how to parse a CSV file.

I am using httpClient:

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
    // need to get CSV values only
}
Daniel
  • 9,491
  • 12
  • 50
  • 66
  • The method i use is like this: var result = JsonConvert.DeserializeObject(response.Content.ReadAsStreamAsync()) –  Feb 22 '22 at 06:12
  • I think you still might use libraries like CsvHelper (https://joshclose.github.io/CsvHelper/getting-started/). It offers you the feature to use a stream: `using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))` where `reader` is a `StreamReader`. – jasdefer Feb 22 '22 at 06:16
  • 1
    You have a multipart response. .net does not have built in way to parse it easily. But there are a lot of NuGet packages to parse such response. Take a look at this thread: https://stackoverflow.com/questions/3880530/are-there-any-multipart-form-data-parser-in-c-sharp-no-asp – NazaRN Feb 22 '22 at 06:19
  • @NazaRN that pointed the right direction! Thanks a lot. Will post a solution. – Daniel Feb 22 '22 at 06:53

1 Answers1

0

In the end I used Http-Multipart-Data-Parser. Credits go to nazarn for giving the hint that there is no default solution and using a package.

var client = new HttpClient();
client.BaseAddress = new Uri("<BASEURL>");

using var content = new MultipartFormDataContent();
var uri = new Uri(client.BaseAddress, "<ENDPOINT>");
var response = await client.PostAsync(uri, content);

response.EnsureSuccessStatusCode();

using (Stream stream = await response.Content.ReadAsStreamAsync())
{
    var parser = await MultipartFormDataParser.ParseAsync(stream);

    // CSV contents in Last file
    var file = parser.Files.Last();
    string filename = file.FileName;
    Stream data = file.Data;

    using (Stream fileStream = File.Create($@"C:\temp\{filename}"))
    {
        data.Seek(0, SeekOrigin.Begin);
        data.CopyTo(fileStream);
    }
}
Daniel
  • 9,491
  • 12
  • 50
  • 66