Accessing ClearQuest programatically
Hi,
I need to access ClearQuest programatically using C#. I just found ClearQuest OSLC REST API. Client is planning to use ClearQuest web 7.1.1. I read that it comes with OSLC REST API out the box. Can someone please provide me some examples on how I can access the CQ through C# or Java using this API. I much appreciate any info. Thank you, Ana |
24 answers
Hi Yuhong,
Thanks for the code but I am getting the same error as skybanter. Could you please help with this error. Thanks, Ana |
Hello,
I probably should have included the "connecting to CQ" code. Here is what I did # Connecting to CQ my $header = HTTP::Headers->new; $header->header(Authorization => "Basic ".encode_base64("$CQ_USERNAME:$CQ_USERNAME", "")); $header->header('Content-Type' => "application/xml; charset=UTF-8"); $header->header(Accept => "application/xml"); my $request = HTTP::Request->new( GET => "$url", $header); my $response = LWP::UserAgent->new->request($request); my $g_cookie_jar->extract_cookies($response); # Posting an attachment ... $request->content($content); $g_cookie_jar->add_cookie_header($request); my $response = LWP::UserAgent->new->request($request); Can you try this first? Thanks - Yuhong Yuhong Yin CC/CQ CM Server & Integrations |
Hi Yuhong,
Thanks... but this line is not syntactically correct: my $g_cookie_jar->extract_cookies($response); It should not have the "my": $g_cookie_jar->extract_cookies($response); Also, what is the value of your $url variable? I set it to: my $url = "http://${CQWEB_HOST}:${CQWEB_PORT}/${APP_CONTEXT}/repo/${CQWEB_REPO}/db/${CQWEB_DB}"; But I still get the same error. I do not think this is an authentication issue. I think something is wrong with one of the properties associated with the attachment. Could you please verify "description", "file", "filename"? Thanks... |
Hello
I am attaching my perl script here. ##################################################################### # Required Perl Modules ##################################################################### use MIME::Base64; use MIME::Lite; use HTTP::Request; use LWP 5.64; use XML::Simple; use Data::Dumper; use HTTP::Request::Common; use LWP::ConnCache; use HTTP::Cookies; ### Info about server and port $SERVER="http://qwin115.ratl.swg.usma.ibm.com:12080"; ### Inf about OSLC url ### for 7.1.2 or later CQ version, it is "cqweb/oslc" ### for 7.1.1 or 7.1.1.x, it is "oslc/cqrest" $OSLC_PATH = "cqweb/oslc"; ### Info about repo $REPO_PATH= "repo/7.0.0/db/SAMPL"; ### Form base URL $URL_BASE = "$SERVER/$OSLC_PATH/$REPO_PATH"; ### CQ username and password $CQ_USERNAME = "yyin"; $CQ_USERNAME = "yyin"; $g_connection_status; $g_cookie_jar; ##------------------------------------------------------------------ ## Main code ##------------------------------------------------------------------ { $g_cookie_jar = HTTP::Cookies->new; $g_connection_status = ""; print "\n Connecting to ClearQuest ...\n"; printf("URL = $URL_BASE\n"); my $header = HTTP::Headers->new; $header->header(Authorization => "Basic ".encode_base64("$CQ_USERNAME:$CQ_USERNAME", "")); $header->header('Content-Type' => "application/xml; charset=UTF-8"); $header->header(Accept => "application/xml"); my $request = HTTP::Request->new( GET => "$URL_BASE", $header); my $response = LWP::UserAgent->new->request($request); $g_cookie_jar->extract_cookies($response); $g_connection_status = $response->status_line; if($g_connection_status eq '200 OK') { print "\nConnected to Clearquest.\n"; printf("\n\nModifying ClearQuest Record by adding an attachment\n"); my $header = HTTP::Headers->new; $header->header(Authorization => "Basic ".encode_base64("$CQ_USERNAME:$CQ_USERNAME", "")); $header->header('Content-Type' => "multipart/form-data; boundary=UdCwYxcWhVYyLPJCRxQTEowAxgcepp"); $link=$URL_BASE."/record/16777224-33554479/field/Attachments/attachment"; printf("post context = $link\n"); my $request = HTTP::Request->new( POST => "$link", $header); my $content = "\r\n"; $content = $content . "Content-Disposition: form-data; name=\"description\""; $content = $content . "\r\n"; $content = $content . "Content-Type: text/plain; charset=US-ASCII"; $content = $content . "\r\n"; $content = $content . "Content-Transfer-Encoding: 8bit"; $content = $content . "\r\n"; $content = $content . "\r\n"; $content = $content . "Test Description"; $content = $content . "\r\n"; $content = $content . "--UdCwYxcWhVYyLPJCRxQTEowAxgcepp"; $content = $content . "\r\n"; $content = $content . "Content-Disposition: form-data; name=\"file\"; filename=\"test6.txt\""; $content = $content . "\r\n"; $content = $content . "Content-Type: application/octet-stream; charset=ISO-8859-1"; $content = $content . "\r\n"; $content = $content . "Content-Transfer-Encoding: binary"; $content = $content . "\r\n"; $content = $content . "\r\n"; $content = $content . "this is a test attachment file."; $content = $content . "\r\n"; $content = $content . "--UdCwYxcWhVYyLPJCRxQTEowAxgcepp--"; $content = $content . "\r\n"; $request->content($content); $g_cookie_jar->add_cookie_header($request); my $response = LWP::UserAgent->new->request($request); if ($response->status_line eq '201 Created') { print "\n\nRecord modified successfully!\n"; } else { $s = $response->status_line; printf("status = $s \n"); } } } Thanks - Yuhong Yuhong Yin CC/CQ CM Server & Integrations |
(I am checking your script) but here is the output when I ran my script.
>perl jazz_sample_oslc_attachmentTest.pl Connecting to ClearQuest ... URL = http://qwin115.ratl.swg.usma.ibm.com:12080/cqweb/oslc/repo/7.0.0/db/SAMPL Connected to Clearquest. Modifying ClearQuest Record by adding an attachment post context = http://qwin115.ratl.swg.usma.ibm.com:12080/cqweb/oslc/repo/7.0.0/ db/SAMPL/record/16777224-33554479/field/Attachments/attachment Record modified successfully! Thanks - Yuhong Yuhong Yin CC/CQ CM Server & Integrations |
Hi Yuhong,
Your script works beautifully! Thanks so much for all your effort with me and Ana. Now I just need to figure out why my original one does not (yet) work. Thanks Again!!! |
Hi Yuhong,
My description field is not being set. Do you see the same behavior? Thanks! |
Yuhong,
Thank you so much. Couple of typos got me the error. But description is not being added (hope code is not checking the value agianst a typo :D ). Skybanter, You have a typo in your code in the uri. You used Attachements instead of Attachments, extra e in the middle. That should fix your code. (I am checking your script) but here is the output when I ran my script. |
Hello
I missed two lines that are required to get the description set. Please add the following lines right after my $content = "\r\n" and before the description line. $content = $content . "--UdCwYxcWhVYyLPJCRxQTEowAxgcepp"; $content = $content . "\r\n"; I have double checked that with this, the description is correctly set. Please note, if you run the script second time against the same record, you need to change the filename to a different one. Thanks - Yuhong Yuhong Yin CC/CQ CM Server and Integrations |
Hi Yuhong,
Thanks for getting back with description fix. It worked like a charm. I have one more favor to ask, how can I delete a attachment through the perl code? I am not sure what URI to use. I tried couple of them and they bombed out with error 405. Thank you for your help, Ana |
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.