CA Service Management

Expand all | Collapse all

Update ticket using REST

Jump to Best Answer
  • 1.  Update ticket using REST

    Posted 04-24-2017 01:55 PM

    I'm trying to port the java example for updating an incident to C Sharp using HTTPWebRequest and Response to make the calls and handle the returns. I'm currently getting a 400 "Bad Request" error using the code below, can someone explain what I need to change for it to work correctly?

    The write doesn't seem to throw any errors, the code where it fails is the first line within the if (put.HaveResponse){.....} 

     

         private static void restPut(string accessKey)
            {
                
                
            try
            {            
             //////////////////////////////////////////////////////
            // PUT OPERATION -- Update an existing Incident ticket
            //////////////////////////////////////////////////////
            string url = "https://servername:8050/caisd-rest";
            string endpointPUT = url + "/in/400001";

            
                string postBody = @"<in>" + "<summary>Updated from REST API Java Samples code</summary>" + "</in>";
                byte[] dataByte = Encoding.ASCII.GetBytes(postBody);
            
            
            //PutMethod put = new PutMethod(endpointPUT);
           HttpWebRequest put = (HttpWebRequest)WebRequest.Create(endpointPUT);
            put.Method = "PUT";
            put.Headers.Add("X-AccessKey", accessKey);
            put.Accept = "application/json";
            put.ContentType = "application/xml; charset=UTF-8";
            //set the length of message body, it defaults to -1 meaning no data to send
            put.ContentLength = dataByte.Length;     
               
              // Get the request stream
              Stream PUTstream =  put.GetRequestStream();
                    // Write the data bytes in the request stream
              PUTstream.Write(dataByte, 0, dataByte.Length);
       
            if (put.HaveResponse) {
                
                HttpWebResponse PUTResponse = (HttpWebResponse)put.GetResponse();
                StreamReader reader = new StreamReader(PUTResponse.GetResponseStream(), false);
       
                //responseString is the full responseString
                string response = reader.ReadToEnd().ToString();
                
            } else {
                        Console.WriteLine("invalid response");
            }
            
            


              //  PUTstream.Close();
            }
            catch (WebException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                Console.Write(e.StackTrace);
            }
            finally
            {
                //release system resources here
            }



  • 2.  Re: Update ticket using REST
    Best Answer

    Posted 05-02-2017 11:39 AM

    I figured out that the 400 error was caused by required fields I wasn't accounting for in my request.

     

    I posted a working example below of using a put request to update a request in case some one else wants to use C# to call rest in the future.

    try
    {
    //////////////////////////////////////////////////////
    // PUT OPERATION -- Update an existing Incident ticket
    //////////////////////////////////////////////////////
    string url = "https://servername:8050/caisd-rest";
    string endpointPUT = url + "/cr/401552";

     

    //create the body of the put request
    string postBody = @"<cr>" +"<description>Updated from REST API Java Samples code</description>" +
    "<summary>Updated from REST API Java Samples code</summary>" + "</cr>";

     

    //encode it into bytes
    byte[] dataByte = Encoding.ASCII.GetBytes(postBody);

     

    //Create an HttpWebRequest and set the headers
    HttpWebRequest put = (HttpWebRequest)WebRequest.Create(endpointPUT);
    put.Method = "PUT";
    put.Headers.Add("X-AccessKey", accessKey);

     

    //set the accept property to return json. Accept is easier to use with HttpWebRequest than WebRequest
    put.Accept = "application/json";
    put.ContentType = "application/xml; charset=UTF-8";

     

    //set the length of message body, it defaults to -1 meaning no data to send
    put.ContentLength = dataByte.Length;

    // Get a request stream to use to write data to the resource
    Stream PUTstream = put.GetRequestStream();

     

    // Write the byte data in the request stream to the resource
    PUTstream.Write(dataByte, 0, dataByte.Length);

     

    // Get the response back from the resource
    HttpWebResponse PUTResponse = (HttpWebResponse)put.GetResponse();

    //use a stream reader to get the response stream from the HttpWebResponse object
    StreamReader reader = new StreamReader(PUTResponse.GetResponseStream(), false);

    //response is the full response string converted to string
    string response = reader.ReadToEnd().ToString();

     

    //verify the response
    Console.WriteLine(response);
    }
    catch (WebException e)
    {
    Console.WriteLine(e.ToString());
    Console.Write(e.StackTrace);
    }
    catch (IOException e)
    {
    Console.WriteLine(e.ToString());
    Console.Write(e.StackTrace);
    }
    finally
    {
    //release system resources here
    }

    }



  • 3.  Re: Update ticket using REST

    Broadcom Employee
    Posted 05-04-2017 12:25 PM

    Thanks for posting a working example Elwynn!

     

    Definitely will help other members in communities!