示例#1
0
        /// <summary>
        /// Sets the request body as form url encoded.
        /// Only valid for Http Methods that allow a request body.
        /// Any existing content in the RequestBody is overriden.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">Dictionary of data to url encode and append to the body.</param>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetFormUrlEncodedRequestBody(this HttpSettings settings, IDictionary <string, string> data)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            settings.RequestBody = new FormUrlEncodedContent(data).ReadAsByteArrayAsync().Result;
            settings.SetContentType("application/x-www-form-urlencoded");

            return(settings);
        }
示例#2
0
        /// <summary>
        /// Sets the request body from an object. Serialized as JSON
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">The object to set as the request body. It will be serialized to JSON.</param>
        /// <param name="indentOutput">Option to indent the output of the format</param>
        /// <remarks>
        /// This uses the JavascriptSerializer
        /// </remarks>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetJsonRequestBody <T>(this HttpSettings settings, T data, bool indentOutput = true)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var requestBody = JsonEncoder.SerializeObject(data);

            settings.RequestBody = Encoding.UTF8.GetBytes(requestBody);
            settings.SetContentType("application/json");
            return(settings);
        }
示例#3
0
        /// <summary>
        /// Sets the request body from an object. Serialized as JSON
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">The object to set as the request body. It will be serialized to JSON.</param>
        /// <param name="indentOutput">Option to indent the output of the format</param>
        /// <remarks>
        /// This uses the JavascriptSerializer
        /// </remarks>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetJsonRequestBody <T>(this HttpSettings settings, T data, bool indentOutput = true)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var requestBody = new JavaScriptSerializer().Serialize(data);

            if (indentOutput)
            {
                requestBody = FormatJsonOutput(requestBody);
            }

            settings.RequestBody = Encoding.UTF8.GetBytes(requestBody);
            settings.SetContentType("application/json");
            return(settings);
        }