Client code in .NET 3.5

 

namespace WordMerge.EndToEndTests
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Net;
    using Newtonsoft.Json;

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Execute_a_post_request()
        {
            string url = "http://localhost:63544/api/document";
            object result = string.Empty;

            // Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
            using (var client = new WebClient())
            {
                // Set the header so it knows we are sending JSON.
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                // Create the one and only "options" parameter object.
                var dto = new DocumentDto
                {
                    TemplatePath = @"C:\Temp\Templates",
                    DestinationPath = @"C:\Temp\Destination",
                    Data = new string[] { "This", " is", " test", " input", " data." }
                };

                // Serialise the data we are sending in to JSON
                string serialisedData = JsonConvert.SerializeObject(dto);

                // Make the request
                var response = client.UploadString(url, serialisedData);

                // Deserialise the response into a GUID
                result = JsonConvert.DeserializeObject(response);
            }

            Assert.AreEqual(@"Succesfully uploaded: This, is, test, input, data.", result.ToString());
        }
    }

    public class DocumentDto
    {
        public string TemplatePath { get; set; }
        public string DestinationPath { get; set; }
        public string[] Data { get; set; }
    }
}

Server code in .NET 4.5

 

 

namespace Service.api
{
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    public class DocumentController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public string Post([FromBody]JObject jsonData)
        {
            // Convert the dynamic JObject to a DocumentDto object.
            DocumentDto dto = jsonData.ToObject<DocumentDto>();
            
            // Use the given data to create the result.
            string seperator = ",";
            string data = string.Join(seperator, dto.Data.ToList<string>());
            string result = string.Format("Succesfully uploaded: {0}", data);

            return result;
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

    public class DocumentDto
    {
        public string TemplatePath { get; set; }
        public string DestinationPath { get; set; }
        public string[] Data { get; set; }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.