autonomatt
autonomatt

Reputation: 4433

How to create HTTP POST Request Body content with WCF REST (Starter Kit)

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);

Upvotes: 1

Views: 4674

Answers (1)

autonomatt
autonomatt

Reputation: 4433

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

var response = http.Post("users.xml", HttpContent.Create(requestXML.ToString()));

Upvotes: 1

Related Questions