Beispiel #1
0
        public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
        {
            if (body == null)
            {
                return(null);
            }

            var arr     = _serializer.Write(body).AsSegment();
            var content = new ByteArrayContent(arr.Array, arr.Offset, arr.Count);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return(content);
        }
        public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
        {
            if (body == null)
            {
                return(null);
            }

            var content = new StringContent(JsonSerializerSettings != null ? JsonConvert.SerializeObject(body, JsonSerializerSettings) : JsonConvert.SerializeObject(body));

            // Set the default Content-Type header to application/json
            content.Headers.ContentType.MediaType = "application/json";

            return(content);
        }
 public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
 {
     if (body is FileContent fileContent)
     {
         var content = new ByteArrayContent(fileContent.Content);
         content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-header")
         {
             FileName = $"\"{fileContent.FileName}\""
         };
         content.Headers.ContentType = new MediaTypeHeaderValue(fileContent.MimeType);
         return(content);
     }
     return(base.SerializeBody(body, info));
 }
Beispiel #4
0
        public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
        {
            if (System.Collections.Generic.EqualityComparer <T> .Default.Equals(body, default(T)))
            {
                return(null);
            }

            var content = new StringContent(JsonSerializerSettings != null ? JsonConvert.SerializeObject(body, JsonSerializerSettings) : JsonConvert.SerializeObject(body));

            // Set the default Content-Type header to application/json
            content.Headers.ContentType.MediaType = "application/json";

            return(content);
        }
            /// <inheritdoc />
            public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
            {
                if (body == null)
                {
                    return(null);
                }
                JsonSerializerSettings = new JsonSerializerSettings
                {
                    Formatting        = Formatting.None,
                    NullValueHandling = NullValueHandling.Ignore
                };
                var content = new StringContent(JsonConvert.SerializeObject(body, JsonSerializerSettings));

                content.Headers.ContentType.MediaType = "application/json";
                return(content);
            }
Beispiel #6
0
        public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
        {
            if (body == null)
            {
                return(null);
            }

            // Consider caching generated XmlSerializers
            var serializer = new XmlSerializer(typeof(T));

            using (var stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, body);
                var content = new StringContent(stringWriter.ToString());
                // Set the default Content-Type header to application/xml
                content.Headers.ContentType.MediaType = "application/xml";
                return(content);
            }
        }
Beispiel #7
0
        public void UsesRequestBodySerializerIfContentBodyIsObjectAndMethodIsSerialized()
        {
            var    requestInfo = new RequestInfo(HttpMethod.Get, "foo");
            object body        = new object();

            requestInfo.SetBodyParameterInfo(BodySerializationMethod.Serialized, body);

            var requestBodySerializer = new Mock <RequestBodySerializer>();

            this.requester.RequestBodySerializer = requestBodySerializer.Object;

            var info = new RequestBodySerializerInfo(requestInfo, null);

            requestBodySerializer.Setup(x => x.SerializeBody(body, info)).Returns(new StringContent("test")).Verifiable();
            var content = this.requester.ConstructContent(requestInfo);

            requestBodySerializer.Verify();
            Assert.IsType <StringContent>(content);
        }
 public HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
 {
     return(SerializeBodyInternal(body));
 }
Beispiel #9
0
        public override HttpContent SerializeBody <T>(T body, RequestBodySerializerInfo info)
        {
            if (body == null)
            {
                return(null);
            }

            if (body is IFormData form)
            {
                var pairs   = form.GetFormData();
                var content = new MultipartFormDataContent();
                foreach (var pair in pairs)
                {
                    if (pair.Value is MultipartFile file)
                    {
                        var stream = file.Stream;
                        if (stream.CanSeek)
                        {
                            long remaining = stream.Length - stream.Position;
                            if (remaining > int.MaxValue)
                            {
                                throw new InvalidOperationException("Uploading files larger than Int32.MaxValue bytes is unsupported");
                            }
                            else if (remaining <= 0)
                            {
                                content.Add(new ByteArrayContent(Array.Empty <byte>()), pair.Key, (string)file.Filename);
                                continue;
                            }
                            var arr = new byte[remaining];
                            stream.Read(arr, 0, arr.Length);
                            content.Add(new ByteArrayContent(arr), pair.Key, (string)file.Filename);
                        }
                        else
                        {
                            var buffer = new ResizableMemory <byte>(4096); // 4 KB
                            while (true)
                            {
                                var segment     = buffer.RequestSegment(4096);
                                int bytesCopied = file.Stream.Read(segment.Array, segment.Offset, segment.Count);
                                if (bytesCopied == 0)
                                {
                                    break;
                                }
                                buffer.Advance(bytesCopied);
                            }
                            content.Add(new ByteArrayContent(buffer.ToArray()), pair.Key, (string)file.Filename);
                        }
                    }
                    else
                    {
                        content.Add(new StringContent(_serializer.WriteUtf16String(pair.Value), Encoding.UTF8, "application/json"), pair.Key);
                    }
                }
                return(content);
            }
            else
            {
                var arr     = _serializer.Write(body).AsSegment();
                var content = new ByteArrayContent(arr.Array, arr.Offset, arr.Count);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                return(content);
            }
        }