Performing a GET on the REST API works but a POST fails

143 views
Skip to first unread message

Max Foulds

unread,
Oct 3, 2012, 7:50:44 AM10/3/12
to devtarge...@googlegroups.com
I am trying to create a Bug but using the following POST fails with a 401 when a GET does authenticate. I appreciate this is fundamentally a write rather than a read but the AD user role does have Add/Edit permissions. what am i missing?

Thanks

        private static XmlDocument restPost(string url, string userName, string userPassword, string domain, string xmlPost)
        {
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            var xd = new XmlDocument();
            if (request != null)
            {
                request.Method = WebRequestMethods.Http.Post;
                string postData = string.Format(xmlPost);
                byte[] data = Encoding.UTF8.GetBytes(postData);
 
                request.ContentType = "application/xml";
                request.ContentLength = data.Length;
 
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                }
                request.Credentials = new NetworkCredential(userName, userPassword, domain);
                request.PreAuthenticate = true;
                var response = request.GetResponse() as HttpWebResponse;
                var reader = new StreamReader(response.GetResponseStream());
                xd.LoadXml(reader.ReadToEnd());
            }
            return xd;
        }

Alex Fomin

unread,
Oct 5, 2012, 8:48:31 AM10/5/12
to devtarge...@googlegroups.com
Hi Max,

You code is correct. Do you use on-demand or on-site TP? What value do you pass to "domain" parameter? What is the body of a 401 response?

Thanks,
Alex

Max Foulds

unread,
Oct 18, 2012, 4:55:45 AM10/18/12
to devtarge...@googlegroups.com
We are using on-site TP and I am passing in the fully qualified domain name into the post

Alex Fomin

unread,
Oct 18, 2012, 7:23:59 AM10/18/12
to devtarge...@googlegroups.com
Do you use win authentication? If so, what is the body of 401 response?

Max Foulds

unread,
Oct 18, 2012, 11:53:08 AM10/18/12
to devtarge...@googlegroups.com
Yes we are using win authentication and It is responding with a "The remote server returned an error: (401) Unauthorized."

Just to clarify, the same account has no problems doing a request before it tries a post.

Max Foulds

unread,
Oct 18, 2012, 11:54:57 AM10/18/12
to devtarge...@googlegroups.com
That was meant to say no problem doing a GET before it tries to POST :)

Alex Fomin

unread,
Nov 8, 2012, 3:33:56 AM11/8/12
to devtarge...@googlegroups.com
Hi Max,

We still need a full body of 401 response including all headers to determine whether this response is caused by IIS itself (that means that there is some issues with IIS configs) or by TP (in case of bugs). Also have you tried to send POST requests with 3rd party tools like Fiddler? It will insert required Windows credentials if IIS is configured correctly. Also you can sniff the full IIS response using fiddler as well.

If you have any issues with capturing the response body, please contact our support at http://messenger.providesupport.com/messenger/targetprocess.html?ps_s=3dJ1YTrvJr9H so we can do this in live chat session,

Thanks,
Alex, TP Team

Max Foulds

unread,
Nov 8, 2012, 10:02:51 AM11/8/12
to devtarge...@googlegroups.com
Alex

Thanks for the direct toward sniffing at the post. It enabled me to get to the bottom of the problem which was 2 fold. My code that I had posted above was incorrect in that the 2 following lines should have been further up the method logic
                request.Credentials = new NetworkCredential(userName, userPassword, domain);
                request.PreAuthenticate = true;

I noticed that there was a post which failed with "401 - Unauthorized: Access is denied due to invalid credentials" with an empty negotiation string and then there was a second post using a encoded negotiation string which then successfully authenticated however my XML was badly formed as I got a bad response which I managed to fix. Anyway problem now solved. See below for my updated working REST post

        private static XmlDocument restPost(string url, string userName, string userPassword, string domain, string xmlPost)
        {
            var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
            var xd = new XmlDocument();
            if (request != null)
            {
                request.Method = WebRequestMethods.Http.Post;
                string postData = string.Format(xmlPost);
                byte[] data = Encoding.UTF8.GetBytes(postData);

                request.ContentType = "application/xml; charset=utf-8";
                request.ContentLength = data.Length;
Reply all
Reply to author
Forward
0 new messages