From my MVC app I am trying to do a POST request to my Web API app. After receiving the data inside Web API app, when I try to read it like HttpContext.Current.Request.Form["SenderAddress"];
I get an error saying Either BinaryRead, Form, Files, or InputStream was accessed before the internal storage was filled by the caller of HttpRequest.GetBufferedInputStream.
Below is the code for both sending and receiving:
POST request (from MVC):
var form = new MultipartFormDataContent();
var uri = _domain + "/api/email/send";
//adding all properties to the form
if (model.SenderAddress != "" && model.SenderAddress != null)
form.Add(new StringContent(model.SenderAddress), "SenderAddress");
if (model.Recipients.FirstOrDefault() != null)
{
foreach (var recipient in model.Recipients)
form.Add(new StringContent(recipient), "Recipients");
}
if (model.Attachments.FirstOrDefault() != null)
{
foreach (var attachment in model.Attachments)
{
byte[] fileData = null;
using (var binaryReader = new BinaryReader(attachment.InputStream))
{
fileData = binaryReader.ReadBytes(attachment.ContentLength);
}
form.Add(new ByteArrayContent(fileData, 0, fileData.Length), "Attachments", attachment.FileName);
}
}
HttpResponseMessage response = await client.PostAsync(uri, form);
response.EnsureSuccessStatusCode();
string sd = response.Content.ReadAsStringAsync().Result;
Receiving data (at Web API):
var temp3 = HttpContext.Current.Request.Form["SenderAddress"];
Here I get that error. How do I solve it?