EDIT: 明らかに私が持っているものは動作しています。私はシリアル化されたオブジェクトから生成されたXMLを使ってテストしていましたが、これは動作しませんでした。私はもっとちょっと迷惑をかけていましたが、私が作成しているダイナミックXMLを使ってみることにしました。私は同じ名前空間を使用していないうちに、そのようにしようとしていたときだったかもしれないと思うかもしれません... しかし、どのようにシリアライズされたオブジェクトからxmlを使用すると機能しませんか?私はそれがオブジェクトから作成されたときにxmlで異なる名前空間を使用していることに気付きました。何故ですか?オブジェクトがREST APIサービスのパラメータとして発行される
オリジナルの投稿: 作成しているREST APIにカスタムオブジェクトを渡す際に問題が発生しています。今は、WebInvokeメソッドにLead型のオブジェクトをポストすることでテストしようとしています。 私のサービスはWCFで書かれています。 Leadオブジェクトは、私のビジネスロジックレイヤーになる別のクラスライブラリプロジェクトにあります。
通常のASP .netページを使用して投稿しています。下のコードのセクションでは、手動でxmlを構築する方法と、xmlとしてオブジェクトを直列化し、POSTデータとして使用する方法について説明します。いずれの作業も、このエラーが発生しています。 リモートサーバーからエラーが返されました:(400)Bad Request。
私はGETメソッドである私が書いた他のWebInvokeメソッドから呼び出して応答を得ることができるので、サービスが正しく構成されていることは知っています。 サービスインタフェースコード:
[ServiceContract(Namespace = "http://www.mytestnamespace.com/test")]
public interface IRESTService
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "leads",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
string AddLead(Lead lead);
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "lead/{id}",
ResponseFormat = WebMessageFormat.Xml)]
string LeadData(string id);
}
サービスコード:
public class RESTService : IRESTService
{
public string AddLead(Lead lead)
{
return "AddLead Hit";
}
public string LeadData(string id)
{
return "LeadData was hit, id=" + id;
}
}
リードオブジェクトコード:
[DataContract(Name="Lead",
Namespace = "http://www.mytestnamespace.com/test")]
public class Lead
{
[DataMember(Name="FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
[DataMember(Name = "Phone")]
public string Phone { get; set; }
}
そして最後に、私はコードここ
はreleveantコードですaspxページでデータを投稿する:
などtry
{
Lead l = new Lead();
l.FirstName = "John";
l.LastName = "Doe";
l.Email = "[email protected]";
l.Phone = "5555551234";
XmlSerializer ser = new XmlSerializer(typeof(Lead));
StringWriter sw = new StringWriter();
ser.Serialize(sw, l);
string s = sw.ToString();
/*XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.CreateElement("Lead");
root.SetAttribute("xmlns", "http://www.mytestnamespace.com/test");
//root.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
doc.InsertBefore(dec, doc.DocumentElement);
doc.AppendChild(root);
XmlElement firstName = doc.CreateElement("FirstName");
XmlElement lastName = doc.CreateElement("LastName");
XmlElement email = doc.CreateElement("Email");
XmlElement phone = doc.CreateElement("Phone");
firstName.AppendChild(doc.CreateTextNode("John"));
lastName.AppendChild(doc.CreateTextNode("Doe"));
email.AppendChild(doc.CreateTextNode("[email protected]"));
phone.AppendChild(doc.CreateTextNode("8885551234"));
root.AppendChild(firstName);
root.AppendChild(lastName);
root.AppendChild(email);
root.AppendChild(phone);*/
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost:54966/RESTService.svc/leads"));
req.Method = "POST";
req.ContentType = "application/xml";
//byte[] formData = UTF8Encoding.UTF8.GetBytes(doc.InnerXml);
byte[] formData = UTF8Encoding.UTF8.GetBytes(s);
req.ContentLength = formData.Length;
using (Stream post = req.GetRequestStream())
{
post.Write(formData, 0, formData.Length);
}
string result = null;
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}
}
catch (Exception exc)
{
throw exc;
}
XmlSerializerを使用してLeadオブジェクトをシリアル化し、サービスでDataContractSerializerを使用してシリアル化を解除すると、400のBad要求エラーが発生することが考えられます。オブジェクトでDataContractSerializerを使用してみてください。 – Rajesh