0

I was trying to attach a csv file as a body parameter in my test script. But still as per the below code controller expect file and just curious how should I pass that.

I run test script in below order

Method-1

public void AttachedRatesFile(string fileName) { _form = string.IsNullOrWhiteSpace(fileName) ? _form = new StringContent(string.Empty) : _form = new StreamContent(File.OpenRead($"{ResourceFolder}{fileName}")); _form.Headers.ContentType = new MediaTypeHeaderValue("application/csv"); _form.Headers.ContentDisposition = new ContentDispositionHeaderValue(fileName); } 

Method-2

 public void PostRequestExecutes(string resource) { var content = new MultipartFormDataContent{_form}; WhenThePostRequestExecutesWithContent(resource, content); } 

Method-3

public async void WhenThePostRequestExecutesWithContent(string resource, HttpContent content) { ResponseMessage = await HttpClient.PostAsync(resource, content); } 

I see null in below file parameter

Controller:

public async Task<IActionResult> SeedData(IFormFile file) { var result = await _seedDataService.SeedData(file); return Ok(new { IsUploadSuccesful = result}); } 

1 Answer 1

1

I would add that to the body as a stream

var memoryContentStream = new MemoryStream(); using (var streamWriter = new StreamWriter(memoryContentStream, Encoding.UTF8, 1000, true)) { using (var jsonTextWriter = new JsonTextWriter(streamWriter)) { var jsonSerializer = new JsonSerializer(); jsonSerializer.Serialize(jsonTextWriter, OBJECT); jsonTextWriter.Flush(); } } if (memoryContentStream.CanSeek) { memoryContentStream.Seek(0, SeekOrigin.Begin); } 

Then

using (var streamContent = new StreamContent(memoryContentStream)) { streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = streamContent; using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { var stream = await response.Content.ReadAsStreamAsync(); response.EnsureIsSuccessStatusCode(); } } 

The above would first write the content as a memory stream and then when creating the POST request you can send the stream as a streamContent

Sign up to request clarification or add additional context in comments.

6 Comments

My file type is csv . Would that make a difference in memory stream code?
If you want to add any type into the body, you should convert it to a stream so it can be sent over.
How does one do this without buffering the body?
@JonathanGilbert You can read the stream after the body has been read rather than the headers in my example. Change the above to HttpCompletionOption.ResponseContentRead. You can also increase the buffer size StreamWriter(memoryContentStream, Encoding.UTF8, BUFFER_SIZE, true)
Huh? I'm asking how you send data in the POST request without it being buffered to an intermediate MemoryStream. The existing HttpContent implementations specifically support: passing a byte[] in as the request body, passing a ReadOnlyMemory<string> as the request body, reading from a Stream to supply the request body, and using System.Text.Json to serialize your data. You have a problem if you want to dynamically emit the data. The solution presented here works around this by emitting the data to a MemoryStream and then using StreamContent. How to avoid this intermediate buffer?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.