I am trying to add document to the index using c# (xml) but I am always getting error 400 (Bad request). Any ideas what I am doing wrong?
Code:
private static string GetXml()
{
XDocument document = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("add",
new XElement("doc",
new XElement("field",
new XAttribute("name", "employeeId"),
new XText("05991")),
new XElement("field",
new XAttribute("name", "skills"),
new XText("Perl"))
)
)
);
return document.ToString(SaveOptions.DisableFormatting);
}
private static void AddDocument()
{
string content = GetXml();
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://mysolrhost:8080/solr/update");
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
byte[] byteArray = Encoding.UTF8.GetBytes(content);
request.ContentLength = byteArray.Length;
using (var requestStream = request.GetRequestStream())
using (var sw = new StreamWriter(requestStream))
{
sw.Write(content);
}
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse) response).StatusDescription);
}
public static void Main(string[] args)
{
AddDocument();
}
Edit: the problem is solved (see the answer below).
Many thanks!