05-18-2011 12:12 PM
I am having a whopper of a time getting hammock to send a post function
I get back this error
"The Request sent by the client is syntactically incorrect (org.xml.sax.SAXParseException: Content is not allowed in prolog.)"
this seems to indicate that my XML body is not formed correctly
here is the code I am using
---------- start clip code ----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Data;
using Hammock;
using Hammock.Authentication;
using Hammock.Web;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace testBed
{
class REST
{
public void delQRest()
{
string auth = "http://" + Config.jsaIP + "/api/hornet-q/queues/jms.queue.deleteq";
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName,
Password = Config.pWord
};
RestClient client = new RestClient
{
Authority = auth,
};
RestRequest request = new RestRequest
{
Credentials = credentials,
Method = WebMethod.Delete
};
RestResponse response = client.Request(request);
Console.WriteLine("the delete Queue status is " + response.StatusCode);
Console.ReadLine();
}// End Method delQRest()
public void makeQRest()
{
try
{
string auth = "http://" + Config.jsaIP + "/api/hornet-q/queues/";
string body = "<?xml version='1.0' standalone='yes'?><queue name='deleteq'><durable>false</durable></queue>";
Serialize eb = new Serialize();
eb.text = body;
BinaryFormatter bFormatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bFormatter.Serialize(ms, eb);
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName,
Password = Config.pWord
};
RestClient client = new RestClient
{
Authority = auth,
};
client.AddHeader("content-type", "application/hornetq.jms.queue+xml");
RestRequest request = new RestRequest
{
Credentials = credentials,
Method = WebMethod.Post,
Entity = ms
};
RestResponse response = client.Request(request);
Console.WriteLine("the create Queue status is " + response.StatusCode);
Console.WriteLine(response.Content);
Console.ReadLine();
} // end try
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}// End Method makeQRest()
}// End class REST
}
------------- end clip -----------
Solved! Go to Solution.
05-19-2011 12:08 AM
Hi Kelly,
I am not very familiar with Hammock. But the error states that the body is the issue. You can try as below
1. Pass body as below (may be something wrong with escape sequences)
"<queue name=\"deleteq\">
<durable>true</durable>
</queue>";
2. Login to the Junos Space through console and go to /var/log/jboss and monitor the server.log. It will give more information on what is wrong with the message so that you can debug
Regards
Yesh
05-20-2011 09:17 AM
Yesh- I have been flounderiing around in the Hammock help files and came up with this and I am embarrassed at how easy that was - I did a search in the Source code I downloaded from the sight here and came up with the RESTClientTest.cs file and that led me to the syntax:
--------- start example --------
public void Can_post_raw_content()
{
var client = new RestClient {Authority = "http://www.apitize.com"};
var request = new RestRequest();
request.AddPostContent(Encoding.UTF8.GetBytes("Bab
--------------- end Example --------
So adding the line
request.AddPostContent(Encoding.UTF8.getBytes(body
to my code and taking out all that failed attempt at serializing and presto it WORKS and my Hornet-Q gets created as it should
Here is my completed code :
------------------ start code clip --------------
public void makeQRest()
{
string auth = "http://" + Config.jsaIP + "/api/hornet-q/queues/";
string body = "<queue name='deleteq'><durable>false</durable></queue>";
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName,
Password = Config.pWord
};
RestClient client = new RestClient
{
Authority = auth,
};
client.AddHeader("content-type", "application/hornetq.jms.queue+xml");
RestRequest request = new RestRequest
{
Credentials = credentials,
Method = WebMethod.Post,
};
request.AddPostContent(Encoding.UTF8.GetBytes(body
RestResponse response = client.Request(request);
Console.WriteLine("the create Queue status is " + response.StatusCode);
Console.WriteLine(response.Content);
Console.ReadLine();
}// End Method makeQRest()
------------------ end code clip -------------
05-20-2011 10:07 AM
Just to make this post a little more complete as a learning tool-----
here is the code I am useing in C# that uses Hammock to send a request to delete the Hornet Q and a request to create the Hornet Q
These are examples of sending a HTTP DELETE and an HTTP POST (with XML in the request body) Below this I will post a code example of an HTTP GET all done with hammock for the RestRequest
--------------- start clip from REST.cs ------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Data;
using Hammock;
using Hammock.Authentication;
using Hammock.Web;
using System.Web;
using System.Xml;
using System.Xml.Linq;
namespace watcher
{
class REST
{
public void delQRest()
{
string auth = "http://" + Config.jsaIP + "/api/hornet-q/queues/jms.queue.deleteq";
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName,
Password = Config.pWord
};
RestClient client = new RestClient
{
Authority = auth,
};
RestRequest request = new RestRequest
{
Credentials = credentials,
Method = WebMethod.Delete
};
RestResponse response = client.Request(request);
Console.WriteLine("the delete Queue status is " + response.StatusCode);
Console.ReadLine();
}// End Method delQRest()
public void makeQRest()
{
try
{
string auth = "http://" + Config.jsaIP + "/api/hornet-q/queues/";
string body = "<queue name='deleteq'><durable>false</durable></queue>";
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName,
Password = Config.pWord
};
RestClient client = new RestClient
{
Authority = auth,
};
client.AddHeader("content-type", "application/hornetq.jms.queue+xml");
RestRequest request = new RestRequest
{
Credentials = credentials,
Method = WebMethod.Post
};
request.AddPostContent(Encoding.UTF8.GetBytes(body
RestResponse response = client.Request(request);
Console.WriteLine("the create Queue status is " + response.StatusCode);
Console.WriteLine(response.Content);
Console.ReadLine();
} // end try
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}// End Method makeQRest()
}// End class REST
}
--------------- end Clip From REST.cs ---------------
---------------- start clip from GetManagedDevices.cs -----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Hammock;
using Hammock.Authentication;
using System.Web;
using System.Xml;
using System.Xml.Linq;
namespace watcher
{
class GetManagedDevices
{
public void CallManDevices()
{
IWebCredentials credentials = new Hammock.Authentication.Basic.BasicAuthCredentials
{
Username = Config.uName, //"super",
Password = Config.pWord //"juniper123"
};
RestClient client = new RestClient
{
Authority = "http://"+Config.jsaIP+"/api/space/device-manageme
};
client.AddHeader("accept", "application/vnd.net.juniper.space.device-manageme
RestRequest request = new RestRequest
{
Credentials = credentials
};
RestResponse response = client.Request(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string fname = Config.jsFolder+"manDevices.xml";
XDocument doc = XDocument.Parse(response.Content);
//write doc to file
try
{
using (XmlTextWriter writer = new XmlTextWriter(fname, null))
{
writer.Formatting = Formatting.Indented;
doc.Save(writer);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
}
}
}
}
------------------ end clip from GetManagedDevices.cs ---------------------
05-21-2011 02:12 AM
Thanks Kelly
05-20-2013 02:26 PM
Hi Kelly
What version of hammock are you using? I'm xlating to VB, but Hammock.Authentication.Basic.BasicAuthCredentials doesn't seem to exist.