Esempio n. 1
0
        /// <summary>
        /// HTTP POST or other send with byte content, e.g. for files or gzipped content
        /// For a more detailed documentation see: https://github.com/FrendsPlatform/Frends.Web#HttpRequestBytes
        /// </summary>
        /// <param name="input">Input parameters</param>
        /// <param name="options">Optional parameters with default values</param>
        /// <returns>Object with the following properties: string Body, Dictionary(string,string) Headers. int StatusCode</returns>
        public static async Task <object> HttpSendBytes([PropertyTab] ByteInput input, [PropertyTab] Options options, CancellationToken cancellationToken)
        {
            var httpClient = GetHttpClientForOptions(options);
            var headers    = GetHeaderDictionary(input.Headers);

            using (var content = GetContent(input))
            {
                var responseMessage = await GetHttpRequestResponseAsync(
                    httpClient,
                    input.Method.ToString(),
                    input.Url,
                    content,
                    headers,
                    options,
                    cancellationToken)
                                      .ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                var response = new HttpResponse()
                {
                    Body       = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false),
                    StatusCode = (int)responseMessage.StatusCode,
                    Headers    = GetResponseHeaderDictionary(responseMessage.Headers, responseMessage.Content.Headers)
                };

                if (!responseMessage.IsSuccessStatusCode && options.ThrowExceptionOnErrorResponse)
                {
                    throw new WebException($"Request to '{input.Url}' failed with status code {(int)responseMessage.StatusCode}. Response body: {response.Body}");
                }

                return(response);
            }
        }
Esempio n. 2
0
 private static HttpContent GetContent(ByteInput input)
 {
     return(new ByteArrayContent(input.ContentBytes));
 }