tags:

views:

754

answers:

1

I'm integrating an app with ZenDesk. They have a REST API. I need to send POX in the request body. I'm using the WCF REST Starter Kit.

How do I programmatically add my xml to the request body?

Here's my unit test:

        [Test]
        public void Can_create_user()
        {
            // Arrange
            http = new HttpClient("http://myapp.zendesk.com/");
            http.TransportSettings.Credentials = new NetworkCredential("[email protected]", "passW0rd");
            http.DefaultHeaders.Accept.Add("application/xml");
            var form = new HttpUrlEncodedForm();
            var expectedStatusCode = 201;

            var request = new XDocument(
                new XElement("user",
                             new XElement("email", "[email protected]"),
                             new XElement("name", "Joe User"),
                             new XElement("roles", "4"),
                             new XElement("restriction-id", "4")));

            form.Add("body", request.ToString());

            // Act
            var response = http.Post("users.xml", form.CreateHttpContent());
            var content = response.Content.ReadAsString();

            // Assert
            response.EnsureStatusIs(expectedStatusCode);
+1  A: 

The solution is to use the static method Microsoft.Http.HttpContent.Create()

var response = http.Post("users.xml", HttpContent.Create(requestXML.ToString()));
mattRo55
And also, replace this http.DefaultHeaders.Accept.Add("application/xml");with thishttp.DefaultHeaders.Add("Content-Type", "application/xml; charset=utf-8");
mattRo55