Ejemplo n.º 1
0
        /// <summary>
        /// Adds StringContent containing a json string of the supplied body object
        /// </summary>
        /// <typeparam name="T">The type of body</typeparam>
        /// <param name="request">The SolidHttpRequest</param>
        /// <param name="body">The request body object</param>
        /// <param name="settings">(Optional) JsonSerializerSettings to use to serialize the body object</param>
        /// <returns>SolidHttpRequest</returns>
        public static ISolidHttpRequest WithJsonContent <T>(this ISolidHttpRequest request, T body, JsonSerializerSettings settings = null)
        {
            var json    = JsonConvert.SerializeObject(body, settings ?? request.GetJsonSerializerSettings());
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            return(request.WithContent(content));
        }
        /// <summary>
        /// Adds <see cref="StringContent" /> containing a json string of type <typeparamref name="T" />.
        /// </summary>
        /// <typeparam name="T">The type of body</typeparam>
        /// <param name="request">The <see cref="ISolidHttpRequest" /> that is being extended.</param>
        /// <param name="body">The request body object of type <typeparamref name="T" />.</param>
        /// <param name="contentType">(Optional) The content type header value.</param>
        /// <param name="settings">(Optional) <see cref="JsonSerializerSettings" /> to use to serialize the <paramref name="body" />..</param>
        /// <returns>The <see cref="ISolidHttpRequest" /> so that additional calls can be chained.</returns>
        public static ISolidHttpRequest WithNewtonsoftJsonContent <T>(this ISolidHttpRequest request, T body, string contentType = "application/json", JsonSerializerSettings settings = null)
        {
            if (settings == null)
            {
                settings = request.Services.GetService <IOptions <SolidHttpNewtonsoftJsonOptions> >().Value.SerializerSettings;
            }

            var json    = JsonConvert.SerializeObject(body, settings);
            var content = new StringContent(json, Encoding.UTF8, contentType);

            return(request.WithContent(content));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds StringContent containing a json string of the supplied body object
        /// </summary>
        /// <typeparam name="T">The type of body</typeparam>
        /// <param name="request">The SolidHttpRequest</param>
        /// <param name="body">The request body object</param>
        /// <param name="settings">(Optional) DataContractSerializerSettings to use to serialize the body object</param>
        /// <returns>SolidHttpRequest</returns>
        public static ISolidHttpRequest WithXmlContent <T>(this ISolidHttpRequest request, T body, DataContractSerializerSettings settings = null)
        {
            using (var ms = new MemoryStream())
            {
                var ser = new DataContractSerializer(typeof(T), settings ?? request.GetXmlSerializerSettings());
                ser.WriteObject(ms, body);
                ms.Position = 0;

                using (var reader = new StreamReader(ms))
                {
                    var content = new StringContent(reader.ReadToEnd(), Encoding.UTF8, "application/xml");
                    return(request.WithContent(content));
                }
            }
        }
        /// <summary>
        /// Adds <see cref="StreamContent" /> containing UTF-8 serialized JSON of type <typeparamref name="T" />.
        /// </summary>
        /// <typeparam name="T">The type of body</typeparam>
        /// <param name="request">The <see cref="ISolidHttpRequest" /> that is being extended.</param>
        /// <param name="body">The request body object of type <typeparamref name="T" />.</param>
        /// <param name="contentType">(Optional) The content type header value.</param>
        /// <param name="options">(Optional) <see cref="JsonSerializerOptions" /> to use to serialize the <paramref name="body" />..</param>
        /// <returns>The <see cref="ISolidHttpRequest" /> so that additional calls can be chained.</returns>
        public static ISolidHttpRequest WithJsonContent <T>(this ISolidHttpRequest request, T body, string contentType = "application/json", JsonSerializerOptions options = null)
        {
            if (options == null)
            {
                options = request.Services.GetService <IOptions <SolidHttpJsonOptions> >().Value.SerializerOptions;
            }

            var bytes   = JsonSerializer.SerializeToUtf8Bytes <T>(body, options);
            var stream  = new MemoryStream(bytes);
            var content = new StreamContent(stream);

            content.Headers.ContentType = new MediaTypeHeaderValue(contentType)
            {
                CharSet = "utf-8"
            };
            return(request.WithContent(content));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds <see cref="StreamContent" /> containing UTF-8 serialized XML of type <typeparamref name="T" />.
        /// </summary>
        /// <typeparam name="T">The type of body</typeparam>
        /// <param name="request">The <see cref="ISolidHttpRequest" /> that is being extended.</param>
        /// <param name="body">The request body object of type <typeparamref name="T" />.</param>
        /// <param name="contentType">(Optional) The content type header value.</param>
        /// <param name="settings">(Optional) <see cref="DataContractSerializerSettings" /> to use to serialize the <paramref name="body" />.</param>
        /// <returns>The <see cref="ISolidHttpRequest" /> so that additional calls can be chained.</returns>
        public static ISolidHttpRequest WithXmlContent <T>(this ISolidHttpRequest request, T body, string contentType = "application/xml", DataContractSerializerSettings settings = null)
        {
            if (settings == null)
            {
                settings = request.Services.GetService <IOptions <SolidHttpXmlOptions> >().Value.SerializerSettings;
            }

            var stream     = new MemoryStream();
            var serializer = new DataContractSerializer(typeof(T), settings);

            serializer.WriteObject(stream, body);
            stream.Position = 0;

            var content = new StreamContent(stream);

            content.Headers.ContentType = new MediaTypeHeaderValue(contentType)
            {
                CharSet = "utf-8"
            };
            return(request.WithContent(content));
        }