21

Hello lovely people of Stack Overflow. Since yesterday I have a problem and I have been browsing SO since then. I have a UWP Client and ASP.NET Core Web Api. I just want to send a stream to my web api but indeed this occurred to be harder task than i thought.

I have a class which I have only one property. The Stream property as you can see below:

public class UploadData
{
    public Stream InputData { get; set; }
}

Then Here is my code from my Web Api:

// POST api/values
[HttpPost]
public string Post(UploadData data)
{
    return "test";
}

I have tried to read the stream From body but the result is same. I can hit the post method UploadData is not null but my InputData is always null.

Here is my UWP's code for post request.

private async void PostStreamButton_OnClick(object sender, RoutedEventArgs e)
{
    using (var client = new HttpClient())
    {
        var dummyBuffer = new UnicodeEncoding().GetBytes("this is dummy stream");
        var dummyStream = new MemoryStream(dummyBuffer).AsRandomAccessStream().AsStream();

        var requestContent = new MultipartFormDataContent();
        var inputData = new StreamContent(dummyStream);
        inputData.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        requestContent.Add(inputData, "inputData");

        HttpResponseMessage response = client.PostAsync("url", inputData).Result;
    }
}

I have tried various of content types which none of them seems to work and I have no idea why. I would really appreciate all the help.

1 Answer 1

20

On client side send the stream content not the whole model.

private async void PostStreamButton_OnClick(object sender, RoutedEventArgs e) {
    using (var client = new HttpClient()) {
        var dummyBuffer = new UnicodeEncoding().GetBytes("this is dummy stream");
        var dummyStream = new MemoryStream(dummyBuffer).AsRandomAccessStream().AsStream();

        var inputData = new StreamContent(dummyStream);

        var response = await client.PostAsync("url", inputData);
    }
}

NOTE: Do not mix .Result blocking calls with async calls. Those tend to cause deadlocks.

On server update action

// POST api/values
[HttpPost]
public IActionResult Post() {
    var stream = Request.Body;
    return Ok("test");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hello, I dont have Content property on my Request. I only have body.
@HasanHasanov, You are correct. My code was for previous version. Body is what you should be looking for.
Out of curiosity, how does this work behind the scenes. When you get a StreamContent (or any streamable thing) as an input/output of a service call, does the request/response just stay open until you dispose the stream object? Trying to visualize how this works behind the scenes.
@JeremyArmstrong take a look at the StreamContent class for client side. pay attention to SerializeToStreamAsync method
On the server side it keeps the stream open till it is done with the request. if it closes it before the request/response is complete, not all the data will be sent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.