It's all about the answers!

Ask a question

Can't create workitem via webrequest in RTC using C#


Thiago Przybylovicz (1113) | asked Jun 19 '17, 2:08 p.m.
edited Jun 20 '17, 3:56 p.m.
 I'm trying to create a .NET web application integration with RTC, where I would insert new workitems using RTC change management services, as defined in this article (specifically in "Create a Change Request"). I was able to get the URL-to-be-used inside services.xml file (/oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/) and my goal is to insert data using JSON.
My code is basically the following:

CookieContainer cookies = new CookieContainer();
HttpWebRequest documentPost = (HttpWebRequest)WebRequest.Create(rtcServerUrl + "/oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/order");//"Order" is the workitem name
documentPost.Method = "POST";
documentPost.CookieContainer = cookies;
documentPost.Accept = "application/json";
documentPost.ContentType = "application/x-oslc-cm-change-request+json";
documentPost.Timeout = TIMEOUT_SERVICO;
string json = "{ \"dc:title\":\"" + title + "\", \"rtc_cm:filedAgainst\": [     { \"rdf:resource\" : \"" + rtcServerUrl + "/resource/itemOid/com.ibm.team.workitem.Category/" + idCategory + "\"} ] }"; //dc:title and rtc_cm:filedAgainst are the only two mandatory data from the workitem I'm trying to create
using (var writer = new StreamWriter(documentPost.GetRequestStream()))
{
    writer.Write(json);
    writer.Flush();
    writer.Close();
}
Encoding encode = System.Text.Encoding.UTF8;
string retorno = null;
//Login
HttpWebRequest formPost = (HttpWebRequest)WebRequest.Create(rtcServerUrl +     "/j_security_check");
formPost.Method = "POST";
formPost.Timeout = TIMEOUT_REQUEST;
formPost.CookieContainer = request.CookieContainer;
formPost.Accept = "text/xml";
formPost.ContentType = "application/x-www-form-urlencoded";
String authString = "j_username=" + userName + "&j_password=" + password;     //create authentication string
Byte[] outBuffer = System.Text.Encoding.UTF8.GetBytes(authString); //store in byte buffer
formPost.ContentLength = outBuffer.Length;
System.IO.Stream str = formPost.GetRequestStream();
str.Write(outBuffer, 0, outBuffer.Length); //update form
str.Close();
//FormBasedAuth Step2:submit the login form and get the response from the     server
HttpWebResponse formResponse = (HttpWebResponse)formPost.GetResponse();
var rtcAuthHeader = formResponse.Headers["X-com-ibm-team-repository-web-    auth-msg"];
//check if authentication has failed
if ((rtcAuthHeader != null) && rtcAuthHeader.Equals("authfailed"))
{
    //authentication failed. You can write code to handle the authentication     failure.
    //if (DEBUG) Console.WriteLine("Authentication Failure");
}
else
{
    //login successful
    HttpWebResponse responseRetorno =     (HttpWebResponse)documentPost.GetResponse();
    if (responseRetorno.StatusCode == HttpStatusCode.Created) { retorno = "[Header] " + responseRetorno.GetResponseHeader("Location") + System.Environment.NewLine; }else
    {
        StreamReader reader = new     StreamReader(responseRetorno.GetResponseStream());
        retorno = "[Response] "  + reader.ReadToEnd();
     }
    responseRetorno.Close();
    formResponse.GetResponseStream().Flush();
    formResponse.Close();
}

As I was managed to search for in other forums, this should be enough in order to create the workitem (I have a very similar code working to update workitems using "" URL and PUT method). However, instead of create the workitem in RTC and give me some response with item's identifier, the request's response returns a huge JSON file that ends with "oslc_cm:next":"https:///oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/%7B0%7D?oslc_cm.pageSize=50&_resultToken=_AAY50FEkEee1V4u7RUQSjA&_startIndex=50. It's a JSON representation of the XML I receive when I access /oslc/contexts/_0_iN4G09EeGGMqpyZT5XdQ/workitems/ directly from browser, like I was trying to do a simple query inside the workitem's collection (even though I'm using POST, not GET).

I also tried to use PUT method, but then I receive a 405 status code.

Does anyone have an idea of what am I missing here? My approach is wrong, even though with the same approach I'm able to update existing workitem data in RTC?

Thanks in advance.

UPDATE (20/06): I created the same code concept in Java and it works. Used "org.apache.http.client" package to execute the request parts.

One answer



permanent link
Donald Nong (14.5k414) | answered Jun 19 '17, 10:54 p.m.

Did you copy & paste your code around and lose track? From you code, I don't now what variable "request" should be, but you should use "documentPost" at the last stage, right?

Also, as it is a POST request for creation, you should detect HttpStatusCode.Created (201) instead of HttpStatusCode.OK (200) for success.


Comments
Thiago Przybylovicz commented Jun 20 '17, 8:27 a.m.

 Hello, Donald. Thanks for your answer.


You're right, my code is bigger than that, so I tried to resume it. I edited the code to make more sense (documentPost, for example, turns into "request" inside "DoRequest" method, which I removed to write this question). 

I also already changed the checking to 201 status code, but this is not the issue, since I keep receiving the status 200 and a big JSON file as if I requested a list of workitems instead of creating a workitem. 

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.