0

For sending data to server I use this solution:

https://stackoverflow.com/a/19664927/8033752

But how to send data to concret end-point RESTful by POST?

Blablacar
  • 583
  • 1
  • 5
  • 16
  • 1
    What do you mean by "restful"? A REST API is just an HTTP endpoint, the REST part doesn't change the semantics of the underlying HTTP request. – CodeCaster Jun 01 '17 at 21:39

1 Answers1

1

This is how I do it - works similarly for a PATCH. Code below optimized to have limited comments and exception handling to demonstrate the principle.

The example is a generic asynchronous method so it can accept any serializable content including single and multiple file streams:

/// <summary>
/// Calls a JSON/REST POST service.
/// </summary>
/// <typeparam name="TValue">Type of to be sent value.</typeparam>
/// <param name="loadPackage">Mandatory. The package the post call shall carry.</param>
/// <param name="requestUri">Mandatory. URI which shall be called.</param>
/// <returns>Returns the plain service response.</returns>
public async Task<HttpResponseMessage> CallPostServicePlainAsync<TValue>(
    TValue loadPackage,
    string requestUri)
{
    using (var httpClient = CreateHttpClient()) // or just `new HttpClient()` plus magic
    {
        bool isStream = typeof(TValue) == typeof(Stream);
        bool isMultipleStreams = typeof(TValue) == typeof(Stream[]);
        if (isStream || isMultipleStreams)
        {
            var message = new HttpRequestMessage();
            message.Method = HttpMethod.Post; // that's what makes it a POST :-)
            var content = new MultipartFormDataContent();
            if (isStream) // single stream
                content.Add(new StreamContent(loadPackage as Stream));
            else if (isMultipleStreams) // this is an array of streams
                foreach (Stream stream in loadPackage as Stream[])
                    content.Add(new StreamContent(stream));
            else // oops
                throw new NotImplementedException("incorrect load package.");
            message.Content = content;
            message.RequestUri = new Uri(requestUri);
            return await httpClient.SendAsync(message).ConfigureAwait(false);
        } else {
            // standard serializable content (not streams)
            ...
        }
    }
}
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62