Junos Space Developer
Reply
Contributor
kelly
Posts: 21
Registered: ‎04-19-2011
0
Accepted Solution

using Hammock to send REST requests

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

Recognized Expert
Yesh
Posts: 456
Registered: ‎09-29-2009
0

Re: using Hammock to send REST requests

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

Contributor
kelly
Posts: 21
Registered: ‎04-19-2011
0

Re: using Hammock to send REST requests

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("Babbabooey!"));

 

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



Contributor
kelly
Posts: 21
Registered: ‎04-19-2011
0

Re: using Hammock to send REST requests

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-management/devices",
            };
            client.AddHeader("accept", "application/vnd.net.juniper.space.device-management.devices+xml;version=1");


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



Recognized Expert
Yesh
Posts: 456
Registered: ‎09-29-2009
0

Re: using Hammock to send REST requests

Thanks Kelly

 

Contributor
ed_gpc
Posts: 195
Registered: ‎09-21-2010
0

Re: using Hammock to send REST requests

Hi Kelly

 

What version of hammock are you using?  I'm xlating to VB, but Hammock.Authentication.Basic.BasicAuthCredentials doesn't seem to exist.

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.