我正在将应用与ZenDesk集成。他们有一个REST API。我需要在请求正文中发送POX。我正在使用WCF REST入门套件。
如何以编程方式将xml添加到请求正文中?
这是我的单元测试:
[Test]
public void Can_create_user()
{
// Arrange
http = new HttpClient("http://myapp.zendesk.com/");
http.TransportSettings.Credentials = new NetworkCredential
("joe@admin.com", "passW0rd");
http.DefaultHeaders.Accept.Add("application/xml");
var form = new HttpUrlEncodedForm();
var expectedStatusCode = 201;
var request = new XDocument(
new XElement("user",
new XElement("email", "joe@user.com"),
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);
答案 0 :(得分:1)
解决方法是使用静态方法Microsoft.Http.HttpContent.Create()
var response = http.Post("users.xml", HttpContent.Create(requestXML.ToString()));